A Typical Hardware System 15 213 CPU chip The course that gives CMU its Zip register file ALU System Level I O Nov 14 2002 memory bus system bus main memory I O bridge bus interface Topics n n n n n n Unix I O Robust reading and writing Reading file metadata Sharing files I O redirection Standard I O I O bus USB controller graphics adapter mousekeyboard disk controller monitor disk 2 class24 ppt Expansion slots for other devices such as network adapters 15 213 F 02 Reading a Disk Sector Step 1 Reading a Disk Sector Step 2 CPU chip 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 register file ALU main memory bus interface 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 I O bus USB controller disk controller monitor mouse keyboard disk 3 graphics adapter disk controller monitor disk 15 213 F 02 4 15 213 F 02 Reading a Disk Sector Step 3 CPU chip register file ALU Unix Files When the DMA transfer completes the disk controller notifies the CPU with an interrupt i e asserts a special interrupt pin on the CPU A Unix file is a sequence of m bytes n B0 B1 Bk Bm 1 All I O devices are represented as files main memory bus interface n dev sda2 usr disk partition n dev tty2 terminal Even the kernel is represented as a file I O bus USB controller mouse keyboard graphics adapter n dev kmem kernel memory image n proc kernel data structures disk controller monitor disk 15 213 F 02 5 Unix I O Unix File Types Regular file n Binary or text file n Unix does not know the difference 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 Directory file n A file that contains the names and locations of other files Basic Unix I O operations system calls Character special and block special files n 7 Opening and closing files n Changing the current file position seek l open and close l lseek not discussed A file type used for interprocess comunication n Reading and writing a file l read and write Socket n n Terminals character special and disks block special FIFO named pipe n 15 213 F 02 6 A file type used for network communication between processes 15 213 F 02 8 15 213 F 02 Opening Files Closing Files Opening a file informs the kernel that you are getting ready to access that file Closing a file informs the kernel that you are finished accessing that file int fd file descriptor int fd file descriptor int retval return value if fd open etc hosts O RDONLY 0 perror open exit 1 if retval close fd 0 perror close 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 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 15 213 F 02 10 Reading Files Writing Files Reading a file copies bytes from the current file position to memory and then updates file position 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 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 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 Returns number of bytes written from buf to file fd 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 Dealing with Short Counts Copying standard input to standard output one byte at a time Short counts can occur in these situations include csapp h int main void char c 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 while Read STDIN FILENO c 1 0 Write STDOUT FILENO c 1 exit 0 n Reading from disk files except for EOF n Writing to disk files How should you deal with short counts in your code n Note the use of error handling wrappers for read and write Appendix B 15 213 F 02 13 n Use the RIO Robust I O package from your textbook s csapp c file Appendix B 15 213 F 02 14 The RIO Package Unbuffered RIO Input and Output 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 Same interface as Unix read and write RIO provides two different kinds of functions n Unbuffered input and output of binary data include csapp h l rio readn and rio writen n Especially useful for transferring data on network sockets ssize t rio readn int fd void usrbuf size t n ssize t rio writen nt fd void usrbuf size t 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 Return num bytes transferred if OK 0 on EOF rio readn only 1 on error l Unlike the Stevens routines the buffered RIO routines are thread safe and can be interleaved arbitrarily on the same descriptor Download from csapp ics code code src src csapp c csapp cs cs cmu cmu edu public edu public ics csapp c csapp ics code include code include csapp csapp h h csapp cs cs cmu cmu edu public edu public ics 15 15 213 F 02 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 Buffered RIO Input Functions …
View Full Document