15 213 The course that gives CMU its Zip System Level I O Nov 14 2002 Topics n n n n n n class24 ppt Unix I O Robust reading and writing Reading file metadata Sharing files I O redirection Standard I O A Typical Hardware System CPU chip register file ALU system bus memory bus main memory I O bridge bus interface I O bus USB controller mousekeyboard 2 graphics adapter disk controller Expansion slots for other devices such as network adapters monitor disk 15 213 F 02 Reading a Disk Sector Step 1 CPU chip register file ALU CPU initiates a disk read by writing a command logical block number and destination memory address to a port address associated with disk controller main memory bus interface I O bus USB controller mouse keyboard graphics adapter disk controller monitor disk 3 15 213 F 02 Reading a Disk Sector Step 2 CPU chip register file ALU Disk controller reads the sector and performs a direct memory access DMA transfer into main memory main memory bus interface I O bus USB controller mouse keyboard graphics adapter disk controller monitor disk 4 15 213 F 02 Reading a Disk Sector Step 3 CPU chip register file ALU When the DMA transfer completes the disk controller notifies the CPU with an interrupt i e asserts a special interrupt pin on the CPU main memory bus interface I O bus USB controller mouse keyboard graphics adapter disk controller monitor disk 5 15 213 F 02 Unix Files A Unix file is a sequence of m bytes n B0 B1 Bk Bm 1 All I O devices are represented as files n dev sda2 usr disk partition n dev tty2 terminal Even the kernel is represented as a file 6 n dev kmem kernel memory image n proc kernel data structures 15 213 F 02 Unix File Types Regular file n Binary or text file n Unix does not know the difference Directory file n A file that contains the names and locations of other files Character special and block special files n Terminals character special and disks block special FIFO named pipe n A file type used for interprocess comunication Socket n 7 A file type used for network communication between processes 15 213 F 02 Unix I O The elegant mapping of files to devices allows kernel to export simple interface called Unix I O Key Unix idea All input and output is handled in a consistent and uniform way Basic Unix I O operations system calls n Opening and closing files l open and close n Changing the current file position seek l lseek not discussed n Reading and writing a file l read and write 8 15 213 F 02 Opening Files Opening a file informs the kernel that you are getting ready to access that file int fd file descriptor if fd open etc hosts O RDONLY 0 perror open exit 1 Returns a small identifying integer file descriptor n fd 1 indicates that an error occurred Each process created by a Unix shell begins life with three open files associated with a terminal n n 9 n 0 standard input 1 standard output 2 standard error 15 213 F 02 Closing Files Closing a file informs the kernel that you are finished accessing that file int fd file descriptor int retval return value if retval close fd 0 perror close exit 1 Closing an already closed file is a recipe for disaster in threaded programs more on this later Moral Always check return codes even for seemingly benign functions such as close 10 15 213 F 02 Reading Files Reading a file copies bytes from the current file position to memory and then updates file position char buf 512 int fd file descriptor int nbytes number of bytes read Open file fd Then read up to 512 bytes from file fd if nbytes read fd buf sizeof buf 0 perror read exit 1 Returns number of bytes read from file fd into buf n n 11 nbytes 0 indicates that an error occurred short counts nbytes sizeof buf are possible and are not errors 15 213 F 02 Writing Files Writing a file copies bytes from memory to the current file position and then updates current file position char buf 512 int fd file descriptor int nbytes number of bytes read Open the file fd Then write up to 512 bytes from buf to file fd if nbytes write fd buf sizeof buf 0 perror write exit 1 Returns number of bytes written from buf to file fd n nbytes 0 indicates that an error occurred n As with reads short counts are possible and are not errors Transfers up to 512 bytes from address buf to file fd 12 15 213 F 02 Unix I O Example Copying standard input to standard output one byte at a time include csapp h int main void char c while Read STDIN FILENO c 1 0 Write STDOUT FILENO c 1 exit 0 Note the use of error handling wrappers for read and write Appendix B 13 15 213 F 02 Dealing with Short Counts Short counts can occur in these situations n Encountering end of file EOF on reads n Reading text lines from a terminal n Reading and writing network sockets or Unix pipes Short counts never occur in these situations n Reading from disk files except for EOF n Writing to disk files How should you deal with short counts in your code n 14 Use the RIO Robust I O package from your textbook s csapp c file Appendix B 15 213 F 02 The RIO Package RIO is a set of wrappers that provide efficient and robust I O in applications such as network programs that are subject to short counts RIO provides two different kinds of functions n Unbuffered input and output of binary data l rio readn and rio writen n Buffered input of binary data and text lines l rio readlineb and rio readnb l Cleans up some problems with Stevens s readline and readn functions l Unlike the Stevens routines the buffered RIO routines are thread safe and can be interleaved arbitrarily on the same descriptor Download from csapp cs cmu edu public ics code src csapp c csapp cs cmu edu public ics code include csapp h 15 15 213 F 02 Unbuffered RIO Input and Output Same interface as Unix read and write Especially useful for transferring data on network sockets include csapp h ssize t rio readn int fd void usrbuf size t n ssize t rio writen nt fd void usrbuf size t n Return num bytes transferred if OK 0 on EOF rio readn only 1 on error 16 n rio readn returns short count only it encounters EOF n rio writen never returns a short count n Calls to rio readn and rio writen can be interleaved arbitrarily on the same descriptor 15 213 F 02 Implementation of rio readn rio readn robustly read n bytes unbuffered ssize t rio readn int fd void usrbuf size t n size t nleft n ssize t nread …
View Full Document