Overview Lecture P3: Unix What is an OS? Files ■ Abstraction for storage (disks). ■ File manipulation commands. Processes. ■ Abstraction for processor (CPU). ■ Some useful commands. Interactions. ■ Between files and processes. ■ I/O redirection and piles. 2 Layers of Abstractions in Unix OS Operating Systems Bare hardware. Multics (1965-1970) Machine language. ■ Ambitious OS project at MIT. Kernel. ■ Pioneered most of innovations in modern OS. User level (C programming). – file system Command level (shell). – protection Window system. – virtual machines Hardware ■ A little ahead of its time. Machine Kernel P User Command Windows 6 7 Unix History Operating Systems Multics (1965-1970). Multics (1965-1970). Unix / Linux (Thompson and Ritchie 1969). Unix / Linux (Thompson and Ritchie 1969). ■ Simplicity and elegance. – C language, bootstrapped implementation DOS. – integrated command structure – simplified, integrated file system Macintosh. – used by most programmers ■ Continued development at AT&T (1970’s) and "shepherding it out." Windows. ■ ■ Berkeley "BSD" (1978-1993): TCP/IP. OS definition under litigation. ■ Various flavors of commercial Unix (1980-1990). ■ Linux gave it new life (1991 - present). 8 12 Files File Manipulation Commands / File. cat, more show the contents of a file % more xx ■ Sequence of bits. ■ A simple and powerful abstraction for bin lib etc u cp, rm, mv copy, remove, move permanent storage (disks). % cp xx yy copy file xx to yy ■ Extended for things beyond disks. % rm xx delete file xx aaclarke cs126 zrnye % rm * delete all files in current directory! "Everything in Unix is a file." % mv xx yy rename file xx to yy ls list file names Directory. files submit %ls list al files in current directory ■ Sequence of files (and other directories). % ls *.c list all files ending in .c %ls-tr list all files, reverse-sorted by date Filename. stock mandel % ls -l list all file details (permissions, size) ■ Sequence of directory names on the path from "/" to the file. mand32.txt /u/cs126/files/mandel/mand32.txt 13 14 File Manipulation Commands Processes mkdir, rmdir make or remove directory Process. % mkdir hello make a new directory named hello ■ An abstraction for the processor (CPU). ■ Almost every command is a process. pwd print name of current (working) directory Over 2,500 standard commands. cd change directory ■ Thousand more available. %cd.. to parent directory ■ EXTENSIBLE: can even add your own. %cd~ to my home directory %cd~xx to xx’s home directory chmod change read/write permissions % chmod 600 hello.c only you can read/write file hello.c % chmod 700 mandel for all files in directory mandel % chmod 644 index.html all Princeton students can read it 15 16 Unix Commands Unix Commands: Text Processing lpr send file to printer grep, awk, perl pattern matching % lpr hello.c print file hello.c sort sort the lines of a file man, apropos online documentation % man ls get help on using ls command diff print out any lines where two files differ cal, date, xclock time utilities emacs, latex text processing % cal 9 2000 display calendar for September, 2000 % emacs hello.c edit file hello.c % date display current date ispell text processing bc, xcalc calculators % ispell readme spell-checker % xcalc graphical version of scientific calculator maple, matlab scientific computing 17 18 Unix Commands: Programming Unix Commands: Specialized for COS 126 emacs, xemacs text processing emacs126, xemacs126 use our customizations as default % emacs hello.c edit file hello.c % xemacs hello.c & cc, lcc, gcc, C compilers enscript126 pretty-print C code g++, javac C++, Java compilers % enscript126 hello.c % gcc hello.c compile C program hello.c gcc126 compile with warnings gdb, jdb C and Java debuggers % gcc126 hello.c submit126 submit COS 126 assignment for grading % submit126 0 hello.c 19 20 Unix Commands: Multimedia Unix Commands: Communication acroread, ghostview display documents mail, pine email % ghostview xx.ps display PostScript file xx.ps % acroread yy.pdf display Acrobat file yy.pdf rn read newsgroups xv, gs display graphics netscape browse web % xv giraffe.gif display graphics file giraffe.gif % gs mand.ps display graphics mand.ps telnet, rlogin, ssh login to remote computer xfig create figures ftp, sftp download files audiotool play or record music soffice StarOffice: free Office clone 21 22 Multiprocessing I/O Redirection and Pipes Abstraction provided by operating system. ■ MULTIPLE "virtual" machines for your use. ■ Outgrowth of 1960s "time-sharing." For COS126. ■ One window for editor. Unix ■ One window for UNIX % emacs hello.c & commands. [1] 18439 Ampersand % netscape & indicates "do this [2] 18434 in the background" % jobs Note: can use ctrl-Z [1] + Running emacs hello.c and bg instead of & [2] - Running netscape 23 24 Filters and Pipes Shell Standard input, standard output. Command interface to UNIX. 3 ■ Abstract files for command interfaces. Just another programming language. mv file1 tmp; Redirection: ■ sequence of instructions a.out > saveanswer mv file2 file1; ■ Standard input from file. ■ variables sort < myfile > myfilesorted mv tmp file2 ■ Standard output to file. ■ branches, loops 4 Shell program to annoy Steve with email Piping: #!/bin/csh -f @ n = 0 ■ Connect standard output of one ls | wc -l > outputfile repeat 5 times while ($n < 5) command to standard input of the plotprog | lpr printf "Goodbye.\n" | next. gamblerall | avg email Shapiro mail -s "yo Hal!" [email protected] @ n = ($n + 1) Don't confuse redirection and piping. sleep 60 plotprog > lpr wait 1 minute end 5 25 26 Shell Graphics EXTENSIBLE: add another command. ANSI C does not directly support graphical output. ■ rename a.out ■ Need help from operating system. ■ or chmod 700 a file containing shell commands ■ In this course we use "PostScript" to get cool pictures. ■ Don’t worry about details yet. Unix Unix % gcc avg.c % mv a.out avg phoenix.Princeton.EDU% cat ifs.ps % gamblerall | avg | lpr %! 50 50 translate 0 0 moveto 512 0 lineto Primary use. 256 512 lineto closepath stroke ■ low overhead "programming" to manipulate files and invoke /pt {0 360 arc fill} def commands 125.0 250.0 5.0 pt 312.5 125.0 5.0 pt 156.2 62.5 5.0 pt 328.1 31.2 5.0 pt 414.1 15.6 5.0 pt showpage phoenix.Princeton.EDU% gs ifs.ps 27 28 Graphics Graphics ifs.c Game played on equilateral triangle, with vertices R, G, B. #include <stdio.h> #include <stdlib.h> ■ Start at R. #include <stdio.h> ■ Repeat the following: #define N 50000 B – pick a random vertex int randomInteger(int n) { ... } – move halfway between current point and vertex int main(void) { 2 int i, r; – draw a "dot" in color of vertex double x = 0.0, y = 0.0, x0, y0; 5 1 for (i = 0; i < N; i++) { What picture r = randomInteger(3); 3 emerges? 6 if (r == 0) { x0 = 0.0; y0 = 0.0; } else if (r == 1) { x0 = 512.0; y0 = 0.0; } 4 else { x0 = 256.0; y0 = 512.0; } x = (x0 + x) / 2.0; 0 y = (y0 + y) / 2.0; R G printf("%f %f\n", x, y); } return 0; } 29 30 Graphics Text output is boring. ■ Replace and add printf() statements to create PostScript. ■ Use gs to view PostScript file. ifs.c . printf("%%!\n 50 50 translate\n"); printf("/pt {0 360 arc fill} def\n"); printf("0 0 moveto 512 0 lineto "); draw enclosing printf("256 512 lineto closepath stroke\n"); triangle for (i = 0; i < N; i++) { . printf("%f %f 1.0 pt\n", x, y); } printf("showpage\n"); 31.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages7 Page
-
File Size-