System-Level I/ONov 14, 2002System-Level I/ONov 14, 2002TopicsTopicsn Unix I/On Robust reading and writingn Reading file metadatan Sharing filesn I/O redirectionn Standard I/Oclass24.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, F’02A Typical Hardware SystemA Typical Hardware SystemmainmemoryI/O bridgebus interfaceALUregister fileCPU chipsystem busmemory busdisk controllergraphicsadapterUSBcontrollermousekeyboard monitordiskI/O busExpansion slots forother devices suchas network adapters.– 3 –15-213, F’02Reading a Disk Sector: Step 1Reading a Disk Sector: Step 1mainmemoryALUregister fileCPU chipdisk controllergraphicsadapterUSBcontrollermousekeyboard monitordiskI/O busbus interfaceCPU initiates a disk read by writing acommand, logical block number, anddestination memory address to a port(address) associated with disk controller.– 4 –15-213, F’02Reading a Disk Sector: Step 2Reading a Disk Sector: Step 2mainmemoryALUregister fileCPU chipdisk controllergraphicsadapterUSBcontrollermousekeyboard monitordiskI/O busbus interfaceDisk controller reads the sector andperforms a direct memory access (DMA)transfer into main memory.– 5 –15-213, F’02Reading a Disk Sector: Step 3Reading a Disk Sector: Step 3mainmemoryALUregister fileCPU chipdisk controllergraphicsadapterUSBcontrollermousekeyboard monitordiskI/O busbus interfaceWhen the DMA transfer completes, thedisk controller notifies the CPU with aninterrupt (i.e., asserts a special “interrupt”pin on the CPU)– 6 –15-213, F’02Unix FilesUnix FilesA Unix A Unix filefile is a sequence of is a sequence of mm bytes: bytes:n B0, B1, .... , Bk , .... , Bm-1All I/O devices are represented as files: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:Even the kernel is represented as a file:n /dev/kmem (kernel memory image)n /proc (kernel data structures)– 7 –15-213, F’02Unix File TypesUnix File TypesRegular fileRegular filen Binary or text file.n Unix does not know the difference!Directory fileDirectory filen A file that contains the names and locations of other files.Character special and block special filesCharacter special and block special filesn Terminals (character special) and disks ( block special)FIFO (named pipe)FIFO (named pipe)n A file type used for interprocess comunicationSocketSocketn A file type used for network communication betweenprocesses– 8 –15-213, F’02Unix I/OUnix I/OThe elegant mapping of files to devices allows kernel toThe elegant mapping of files to devices allows kernel toexport simple interface called Unix I/O.export simple interface called Unix I/O.Key Unix idea: All input and output is handled in aKey Unix idea: All input and output is handled in aconsistent and uniform way.consistent and uniform way.Basic Unix I/O operations (system calls):Basic Unix I/O operations (system calls):n Opening and closing filesl open()and close()n Changing the current file position (seek)l lseek (not discussed)n Reading and writing a filel read() and write()– 9 –15-213, F’02Opening FilesOpening FilesOpening a file informs the kernel that you are gettingOpening a file informs the kernel that you are gettingready to access that file.ready to access that file.Returns a small identifying integer Returns a small identifying integer file descriptorfile descriptorn fd == -1 indicates that an error occurredEach process created by a Unix shell begins life withEach process created by a Unix shell begins life withthree open files associated with a terminal:three open files associated with a terminal:n 0: standard inputn 1: standard outputn 2: standard errorint fd; /* file descriptor */if ((fd = open(Ò/etc/hostsÓ, O_RDONLY)) < 0) { perror(ÒopenÓ); exit(1);}– 10 –15-213, F’02Closing FilesClosing FilesClosing a file informs the kernel that you are finishedClosing a file informs the kernel that you are finishedaccessing that file.accessing that file.Closing an already closed file is a recipe for disaster inClosing an already closed file is a recipe for disaster inthreaded programs (more on this later)threaded programs (more on this later)Moral: Always check return codes, even for seeminglyMoral: Always check return codes, even for seeminglybenign functions such as benign functions such as close()close()int fd; /* file descriptor */int retval; /* return value */if ((retval = close(fd)) < 0) { perror(ÒcloseÓ); exit(1);}– 11 –15-213, F’02Reading FilesReading FilesReading a file copies bytes from the current fileReading a file copies bytes from the current fileposition to memory, and then updates file position.position to memory, and then updates file position.Returns number of bytes read from file Returns number of bytes read from file fdfd into into bufbufn nbytes < 0 indicates that an error occurred.n short counts (nbytes < sizeof(buf) ) are possible andare not errors!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);}– 12 –15-213, F’02Writing FilesWriting FilesWriting a file copies bytes from memory to the current fileWriting a file copies bytes from memory to the current fileposition, and then updates current file position.position, and then updates current file position.Returns number of bytes written from Returns number of bytes written from bufbuf to file to file fdfd..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 Transfers up to 512 bytes from address bufbuf to file to file fdfdchar 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);}– 13 –15-213, F’02Unix I/O ExampleUnix I/O ExampleCopying standard input to standard output one byte at aCopying standard input to standard output one byte at atime.time.Note the use of error handling wrappers for read and writeNote the use of error handling wrappers for read and write(Appendix B).(Appendix B).#include "csapp.h"int main(void) { char c; while(Read(STDIN_FILENO, &c, 1) != 0) Write(STDOUT_FILENO, &c, 1);
View Full Document