System-Level I/O November 12, 2007Unix I/O Key CharacteristicsUnix FilesUnix File TypesUnix I/OOpening FilesClosing FilesReading FilesWriting FilesUnix I/O ExampleDealing with Short CountsThe RIO PackageUnbuffered RIO Input and OutputImplementation of rio_readnBuffered I/O: MotivationBuffered I/O: ImplementationBuffered RIO Input FunctionsRIO ExampleFile MetadataExample of Accessing File MetadataAccessing DirectoriesHow the Unix Kernel Represents Open FilesFile SharingHow Processes Share FilesI/O RedirectionI/O Redirection ExampleI/O Redirection Example (cont)Fun with File Descriptors (1)Fun with File Descriptors (2)Fun with File Descriptors (3)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/OChoosing I/O FunctionsFor Further InformationSystem-Level I/ONovember 12, 2007System-Level I/ONovember 12, 2007TopicsTopicsUnix I/ORobust reading and writingReading file metadataSharing filesI/O redirectionStandard I/O15-213class20.ppt15-213, F’07– 2 –15-213, F’07Unix 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’07Unix 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)– 4 –15-213, F’07Unix 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– 5 –15-213, F’07Unix 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()– 6 –15-213, F’07Opening 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);}– 7 –15-213, F’07Closing 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);}– 8 –15-213, F’07Reading FilesReading FilesReading a file copies bytes from the current file position to Reading a file copies bytes from the current file position to memory, and then updates file position.memory, and then updates file position.Returns number of bytes read from file Returns number of bytes read from file fdfd into into bufbufReturn type ssize_t is signed integernbytes < 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);}– 9 –15-213, F’07Writing 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 file fd.fd.nbytes < 0 indicates that an error occurred.As with reads, short counts are possible and are not errors!Transfers Transfers up toup to 512 bytes from address 512 bytes from address bufbuf to file to file fdfdchar buf[512];int fd; /* file descriptor */int nbytes; /* number
View Full Document