System-Level I/O Nov 14, 2002A Typical Hardware SystemReading a Disk Sector: Step 1Reading a Disk Sector: Step 2Reading a Disk Sector: Step 3Unix FilesUnix File TypesUnix I/OOpening FilesClosing FilesReading FilesWriting FilesUnix I/O ExampleDealing with Short CountsThe RIO PackageUnbuffered RIO Input and OutputImplementation of rio_readnBuffered RIO Input FunctionsRIO ExampleFile MetadataExample of Accessing File MetadataHow the Unix Kernel Represents Open FilesFile SharingHow Processes Share FilesI/O RedirectionI/O Redirection ExampleI/O Redirection Example (cont)Standard I/O FunctionsStandard I/O StreamsBuffering in Standard I/OStandard I/O Buffering in ActionUnix I/O vs. Standard I/O vs. RIOPros and Cons of Unix I/OPros and Cons of Standard I/OPros and Cons of Standard I/O (cont)Slide 36Choosing I/O FunctionsFor Further InformationSystem-Level I/ONov 14, 2002System-Level I/ONov 14, 2002TopicsTopicsUnix I/ORobust reading and writingReading file metadataSharing filesI/O redirectionStandard 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 bus memory busdisk controllergraphicsadapterUSBcontrollermouse keyboard monitordiskI/O busExpansion slots forother devices suchas network adapters.– 3 –15-213, F’02Reading a Disk Sector: Step 1Reading a Disk Sector: Step 1 mainmemoryALUregister fileCPU chipdisk controllergraphicsadapterUSBcontrollermouse keyboard monitordiskI/O busbus interfaceCPU initiates a disk read by writing a command, logical block number, and destination 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 controllergraphicsadapterUSBcontrollermouse keyboard monitordiskI/O busbus interfaceDisk controller reads the sector and performs 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 controllergraphicsadapterUSBcontrollermouse keyboard monitordiskI/O busbus interfaceWhen the DMA transfer completes, the disk controller notifies the CPU with an interrupt (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:B0, B1, .... , Bk , .... , Bm-1All I/O devices are represented as files:All I/O devices are represented as files:/dev/sda2 (/usr disk partition)/dev/tty2 (terminal)Even the kernel is represented as a file:Even the kernel is represented as a file:/dev/kmem (kernel memory image) /proc (kernel data structures)– 7 –15-213, F’02Unix File TypesUnix File TypesRegular fileRegular fileBinary or text file. Unix does not know the difference!Directory fileDirectory fileA file that contains the names and locations of other files.Character special and block special filesCharacter special and block special filesTerminals (character special) and disks ( block special)FIFO (named pipe)FIFO (named pipe)A file type used for interprocess comunicationSocketSocketA file type used for network communication between processes– 8 –15-213, F’02Unix I/OUnix I/OThe elegant mapping of files to devices allows kernel to The elegant mapping of files to devices allows kernel to export simple interface called Unix I/O.export simple interface called Unix I/O.Key Unix idea: All input and output is handled in a Key Unix idea: All input and output is handled in a consistent and uniform way.consistent and uniform way.Basic Unix I/O operations (system calls): Basic Unix I/O operations (system calls): Opening and closing filesopen()and close()Changing the current file position (seek)lseek (not discussed)Reading and writing a fileread() and write()– 9 –15-213, F’02Opening FilesOpening FilesOpening a file informs the kernel that you are getting Opening a file informs the kernel that you are getting ready to access that file.ready to access that file.Returns a small identifying integer Returns a small identifying integer file descriptorfile descriptorfd == -1 indicates that an error occurredEach process created by a Unix shell begins life with Each process created by a Unix shell begins life with three open files associated with a terminal:three open files associated with a terminal:0: standard input1: standard output2: 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 finished Closing a file informs the kernel that you are finished accessing that file.accessing that file.Closing an already closed file is a recipe for disaster in Closing an already closed file is a recipe for disaster in threaded programs (more on this later)threaded programs (more on this later)Moral: Always check return codes, even for seemingly Moral: Always check return codes, even for seemingly benign 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 file position Reading a file copies bytes from the current file position to memory, and then updates file 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 bufbufnbytes < 0 indicates that an error occurred.short counts (nbytes < sizeof(buf) ) are possible and are 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 file Writing a file copies bytes from memory to the current file position, 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
View Full Document