<<

What Is ?

Browser emacs gcc DVD Player Operating Systems, Operating System System Calls, and Buffered I/O Hardware

CS 217

• Abstraction of hardware • Virtualization • Protection and security 1 2

Computing and Communications Academic Computers in 1983 and 2003 Exponential Growth! (Courtesy J. Gray) • Performance/Price doubles every 18 months 1883 2003 Ratio • 100x per decade CPU clock 3Mhz 3Ghz 1:1000 • Progress in next 18 months $/machine $80k $800 100:1 = ALL previous progress DRAM 256k 256M 1:1000 o New storage = sum of all old storage (ever) o New processing = sum of all old processing. Disk 20MB 200GB 1:10,000 • Aggregate bandwidth doubles in 8 months Network BW 10Mbits/sec 1GBits/sec 1:100

Address bits 16-32 32-64 1:2

Users/machine 10s 1 (or < 1) > 10:1

$/Performance $80k < $800/1000 100,000+:1 15 years ago 3 4 Phase 1: Hardware Expensive, Human Cheap Phase 2: Hardware Cheap, Human Expensive

• User at console, OS as • Use cheap terminals to share a computer • Batch monitor (no protection): load, run, print • -sharing OS • Development • enters the mainstream o Data channels, ; overlap I/O and CPU o DMA • Problems: thrashing as the number of users increases o : keep bugs to individual programs o : designed in 1963 and run in 1969 • Assumption: No bad people. No bad programs. Minimum interactions App1 App2 . . . App2 Application OS Time-sharing OS

Hardware Hardware hardware 5 hardware 6

Phase 3: HW Cheaper, Human More Expensive Phase 4: > 1 Machines per User

• Personal computer • Parallel and distributed systems o Altos OS, Ethernet, Bitmap display, laser printer o Parallel machine o Pop-menu window interface, email, publishing SW, spreadsheet, o Clusters FTP, Telnet o Network is the computer Eventually >100M unites per year o • Pervasive computers • PC operating system o Wearable computers o Memory protection o Computers everywhere Multiprogramming o • OS are general and specialized o Networking

7 8 A Typical Operating System Layers of Abstraction

• Abstraction: Layered services to access hardware o We learn how to use the services here COS318 will teach how to implement Appl Prog o User stream • Virtualization: Each user with its “own” machine (COS318) Stdio Library FILE * • Protection & security: make the machine safe (COS318) fd hierarchical file system User User User User Process Process Process Process Kernel Storage variable-length segments

OS Kernel Driver disk blocks

Hardware Disk 9 10

System Calls Mechanism

• Kernel provided system services: “protected” procedure call • Processor modes o User mode: can execute normal instructions and access only user memory Supervisor mode: can execute normal instructions, privileged Appl Prog o fopen,fclose, printf, instructions and access all of memory (e.g., devices) fgetc, getchar,… • System calls user Stdio Library , , , o User cannot execute privileged instructions kernel , seek o Users must ask OS to execute them - system calls File System o System calls are often implemented using traps (int) o OS gains control through , switches to supervisor model, • Unix has ~150 system calls; see performs service, switches back to user mode, and gives control back to user (iret) o man 2 intro o /usr/include/syscall.h

11 12 System-call interface = ADTs open system call

ADT NAME operations open - open and possibly create a file or device • File input/output flags examples: O_RDONLY o open, close, read, write, SYNOPSIS O_WRITE|O_CREATE • Process control #include #include mode is the permissions o , , , , , ... #include to use if file must be created • Interprocess communication int open(const char *pathname, int flags, mode_t mode); o pipe, socket ...

DESCRIPTION The open() system call is used to convert a pathname into a (a small, non-negative integer for use in subsequent I/O as with read, write, etc.). When the call is successful, the file descriptor returned will be . . . 13 14

close system call read System Call

NAME NAME close - close a file descriptor read - read from a file descriptor SYNOPSIS SYNOPSIS int read(int fd, void *buf, int count); int close(int fd); DESCRIPTION read() attempts to read up to count bytes from file descriptor fd DESCRIPTION into the buffer starting at buf. close closes a file descriptor, so that it no longer refers to any file and If count is zero, read() returns zero and has no other results. If may be reused. Any locks held on the file it was associated with, and owned count is greater than SSIZE_MAX, the result is unspecified. by the process, are removed (regardless of the file descriptor that was used RETURN VALUE to obtain the lock) . . . . On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested . . . . On error, -1 is returned, and errno is set 15 appropriately. 16 write System Call Making Sure It All Gets Written

NAME int safe_write(int fd, char *buf, int nbytes) write – write to a file descriptor { SYNOPSIS int n; char *p = buf; int write(int fd, void *buf, int count); char *q = buf + nbytes; DESCRIPTION while (p < q) { write writes up to count bytes to the file referenced by the file descriptor fd if ((n = write(fd, p, (q-p)*sizeof(char))) > 0) from the buffer starting at buf. p += n/sizeof(char); else RETURN VALUE perror(“safe_write:”); On success, the number of bytes written is returned (zero indicates nothing } was written). It is not an error if this number is smaller than the number of return nbytes; bytes requested . . . . On error, -1 is returned, and is set appropriately. errno }

17 18

Buffered I/O Buffered I/O (cont)

• Single-character I/O is usually too slow • Solution: read a chunk and dole out as needed

int getchar(void) { int getchar(void) { char ; static char buf[1024]; if (read(0, &c, 1) == 1) static char *p; return c; static int n = 0; else return EOF; } if (n--) return *p++;

n = read(0, buf, sizeof(buf)); if (n <= 0) return EOF; p = buf; return getchar(); }

19 20 Standard I/O Library Why Is “getc” A ?

#define getc(p) (--(p)->_cnt >= 0 ? \ #define getc(p) (--(p)->_cnt >= 0 ? \ (int)(*(unsigned char *)(p)->_ptr++) : \ (int)(*(unsigned char *)(p)->_ptr++) : \ _filbuf(p)) _filbuf(p))

typedef struct _iobuf { #define getchar() getc(stdin) int _cnt; /* num chars left in buffer */ char *_ptr; /* ptr to next char in buffer */ • Invented in 1970s, when char *_base; /* beginning of buffer */ o Computers had slow function-call instructions int _bufsize;/* size of buffer */ o Compilers couldn’t inline-expand very well short _flag; /* open mode flags, etc. */ char _file; /* associated file descriptor */ • It’s not 1975 any more } FILE; o Moral: don’t invent new macros, use functions

extern FILE *stdin, *stdout, *stderr;

21 22

fopen Stdio library

FILE *fopen(char *name, char *rw) { • fopen, fclose This (large) library interface is not the operating-system interface; • feof, ferror, fileno, fstat Use malloc to create a struct _iobuf much more room for flexibility. o status inquiries This ADT is implemented in terms Determine appropriate “flags” from “rw” parameter •fflush of the lower-level “file-descriptor” make outside world see o ADT. Call open to get the file descriptor changes to buffer Fill in the _iobuf appropriately • fgetc, fgets, fread } • fputc fputs, fwrite • printf, fprintf • scanf, fscanf • fseek • and more ...

23 24 Summary

• OS is the between hardware and applications o Abstraction: provide services to access the hardware o Virtualization: Provides each process with its own machine o Protection & security: make the environment safe • System calls o ADT for the user applications o Standard I/O example o User-level libraries layered on top of system calls

25