What Is Operating System? Operating Systems, System Calls, and Buffered I/O emacs gcc Brwser DVD Player Operating System CS 217 1 Abstractin f hardware Virtualizatin Prtectin and security 2 Academic Cmputers in 1983 and 2003 1883 2003 Rati CPU clck 3Mhz 3Ghz 1:1000 $/machine $80k $800 100:1 DRAM 256k 256M 1:1000 Disk 20MB 200GB 1:10,000 Netwrk BW 10Mbits/sec 1GBits/sec 1:100 Cmputing and Cmmunicatins Expnential Grwth! (Curtesy J. Gray) Perfrmance/Price dubles every 18 mnths 100x per decade Prgress in next 18 mnths = ALL previus prgress New strage = sum f all ld strage (ever) New prcessing = sum f all ld prcessing. Aggregate bandwidth dubles in 8 mnths Address bits 16-32 32-64 1:2 s/machine 10s 1 (r < 1) > 10:1 $/Perfrmance $80k < $800/1000 100,000+:1 3 15 years ag 4
Phase 1: Expensive, Human Cheap Phase 2: Cheap, Human Expensive at cnsle, OS as subrutine library Use cheap terminals t share a cmputer Batch mnitr (n prtectin): lad, run, print Time-sharing OS Develpment Unix enters the mainstream Data channels, interrupts; verlap I/O and CPU DMA Memry prtectin: keep bugs t individual prgrams Multics: designed in 1963 and run in 1969 Prblems: thrashing as the number f users increases Assumptin: N bad peple. N bad prgrams. Minimum interactins Applicatin App1 OS... App2 Time-sharing OS hardware App2 5 hardware 6 Phase 4: > 1 Machines per Phase 3: HW Cheaper, Human Mre Expensive Persnal cmputer Parallel and distributed systems Alts OS, Ethernet, Bitmap display, laser printer Pp-menu windw interface, email, publishing SW, spreadsheet, FTP, Telnet Eventually >100M unites per year Parallel machine Clusters Netwrk is the cmputer Pervasive cmputers PC perating system Wearable cmputers Cmputers everywhere Memry prtectin Multiprgramming Netwrking OS are general and specialized 7 8
A Typical Operating System Layers f Abstractin Abstractin: Layered services t access hardware We learn hw t use the services here COS318 will teach hw t implement Virtualizatin: Each user with its wn machine (COS318) Prtectin & security: make the machine safe (COS318) prcess Kernel Appl Prg Stdi Library File System Strage FILE * stream int fd hierarchical file system variable-length segments OS Kernel Driver disk blcks Disk 9 10 System Calls Kernel prvided system services: prtected prcedure call user kernel Appl Prg Stdi Library File System Unix has ~150 system calls; see man 2 intr /usr/include/syscall.h fpen,fclse, printf, fgetc, getchar, pen, clse, read, write, seek System Call Mechanism r mdes mde: can execute nrmal instructins and access nly user memry Supervisr mde: can execute nrmal instructins, privileged instructins and access all f memry (e.g., devices) System calls cannt execute privileged instructins s must ask OS t execute them - system calls System calls are ften implemented using traps (int) OS gains cntrl thrugh trap, switches t supervisr mdel, perfrms service, switches back t user mde, and gives cntrl back t user (iret) 11 12
System-call interface = ADTs ADT peratins File input/utput pen, clse, read, write, dup cntrl frk, exit, wait, kill, exec,... Interprcess cmmunicatin pipe, scket... pen system call pen - pen and pssibly create a file r device flags examples: O_RDONLY O_WRITE O_CREATE #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> mde is the permissins t use if file must be created int pen(cnst char *pathname, int flags, mde_t mde); 13 The pen() system call is used t cnvert a pathname int a file descriptr (a small, nn-negative integer fr use in subsequent I/O as with read, write, etc.). When the call is successful, the file descriptr returned will be... 14 clse system call clse - clse a file descriptr int clse(int fd); clse clses a file descriptr, s that it n lnger refers t any file and may be reused. Any lcks held n the file it was assciated with, and wned by the prcess, are remved (regardless f the file descriptr that was used t btain the lck).... 15 read System Call read - read frm a file descriptr int read(int fd, vid *buf, int cunt); read() attempts t read up t cunt bytes frm file descriptr fd int the buffer starting at buf. If cunt is zer, read() returns zer and has n ther results. If cunt is greater than SSIZE_MAX, the result is unspecified. RETURN VALUE On success, the number f bytes read is returned (zer indicates end f file), and the file psitin is advanced by this number. It is nt an errr if this number is smaller than the number f bytes requested.... On errr, -1 is returned, and errn is set apprpriately. 16
write System Call write write t a file descriptr int write(int fd, vid *buf, int cunt); write writes up t cunt bytes t the file referenced by the file descriptr fd frm the buffer starting at buf. RETURN VALUE On success, the number f bytes written is returned (zer indicates nthing was written). It is nt an errr if this number is smaller than the number f bytes requested.... On errr, -1 is returned, and errn is set apprpriately. Making Sure It All Gets Written int safe_write(int fd, char *buf, int nbytes) { int n; char *p = buf; char *q = buf + nbytes; while (p < q) { if ((n = write(fd, p, (q-p)*sizef(char))) > 0) p += n/sizef(char); else perrr( safe_write: ); return nbytes; 17 18 Buffered I/O Single-character I/O is usually t slw int getchar(vid) { char c; if (read(0, &c, 1) == 1) return c; else return EOF; Buffered I/O (cnt) Slutin: read a chunk and dle ut as needed int getchar(vid) { static char buf[1024]; static char *p; static int n = 0; if (n--) return *p++; n = read(0, buf, sizef(buf)); if (n <= 0) return EOF; p = buf; return getchar(); 19 20
Standard I/O Library #define getc(p) (--(p)->_cnt >= 0? \ (int)(*(unsigned char *)(p)->_ptr++) : \ _filbuf(p)) typedef struct _ibuf { int _cnt; /* num chars left in buffer */ char *_ptr; /* ptr t next char in buffer */ char *_base; /* beginning f buffer */ int _bufsize;/* size f buffer */ shrt _flag; /* pen mde flags, etc. */ char _file; /* assciated file descriptr */ FILE; Why Is getc A Macr? #define getc(p) (--(p)->_cnt >= 0? \ (int)(*(unsigned char *)(p)->_ptr++) : \ _filbuf(p)) #define getchar() getc(stdin) Invented in 1970s, when Cmputers had slw functin-call instructins Cmpilers culdn t inline-expand very well It s nt 1975 any mre Mral: dn t invent new macrs, use functins extern FILE *stdin, *stdut, *stderr; 21 22 fpen FILE *fpen(char *name, char *rw) { Use mallc t create a struct _ibuf Determine apprpriate flags frm rw parameter Call pen t get the file descriptr Fill in the _ibuf apprpriately Stdi library fpen, fclse fef, ferrr, filen, fstat status inquiries fflush make utside wrld see changes t buffer fgetc, fgets, fread fputc fputs, fwrite printf, fprintf scanf, fscanf fseek and mre... This (large) library interface is nt the perating-system interface; much mre rm fr flexibility. This ADT is implemented in terms f the lwer-level file-descriptr ADT. 23 24
Summary OS is the sftware between hardware and applicatins Abstractin: prvide services t access the hardware Virtualizatin: Prvides each prcess with its wn machine Prtectin & security: make the envirnment safe System calls ADT fr the user applicatins Standard I/O example -level libraries layered n tp f system calls 25