System-Level I/O November 4, 2003Unix I/O Key CharacteristicsA 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 MetadataMetadata as File (Plan 9, ReiserFS 4)Accessing DirectoriesHow 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 39Choosing I/O FunctionsAsynchronous I/OAsynchronous I/O (cont.)For Further InformationSystem-Level I/ONovember 4, 2003System-Level I/ONovember 4, 2003TopicsTopicsUnix I/ORobust reading and writingReading file metadataSharing filesI/O redirectionStandard I/O15-213“The course that gives CMU its Zip!”– 2 –15-213, F’03Unix I/O Key CharacteristicsUnix I/O Key CharacteristicsClassic Unix/Linux I/O:Classic Unix/Linux I/O:I/O operates on linear streams I/O operates on linear streams of Bytesof BytesCan reposition insertion point and extend file at endI/O tends to be synchronousI/O tends to be synchronousRead or write operation block until data has been transferredFine grained I/OFine grained I/OOne key-stroke at a timeEach I/O event is handled by the kernel and an appropriate processMainframe I/O:Mainframe I/O:I/O operates on structured I/O operates on structured recordsrecordsFunctions to locate, insert, remove, update recordsI/O tends to be asynchronousI/O tends to be asynchronousOverlap I/O and computation within a processCoarse grained I/OCoarse grained I/OProcess writes “channel programs” to be executed by the I/O hardwareMany I/O operations are performed autonomously with one interrupt at completion– 3 –15-213, F’03A 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.– 4 –15-213, F’03Reading 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.– 5 –15-213, F’03Reading 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.– 6 –15-213, F’03Reading 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)– 7 –15-213, F’03Unix 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)– 8 –15-213, F’03Unix 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 communicationSocketSocketA file type used for network communication between processes– 9 –15-213, F’03Unix 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()– 10 –15-213, F’03Opening 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);}– 11 –15-213, F’03Closing 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”);
View Full Document