Page 1System-Level I/OOctober 31, 2007System-Level I/OOctober 31, 2007TopicsTopics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O15-213“The course that gives CMU its Zip!”lecture-18.ppt–2–15-213, F’07Unix FilesUnix FilesA Unix A Unix filefileis a sequence of is a sequence of mmbytes: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)–3–15-213, F’07Unix File TypesUnix File TypesRegular fileRegular file Binary or text file. Unix does not know the difference!Directory fileDirectory file A file that contains the names and locations of other files.Character special and block special filesCharacter special and block special files Terminals (character special) and disks ( block special)FIFO (named pipe)FIFO (named pipe) A file type used for interprocess communicationSocketSocket A file type used for network communication between processes–4–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 filesz open()and close() Changing the current file position (seek)z lseek (not discussed) Reading and writing a filez read() and write()–5–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 descriptor fd == -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 input 1: standard output 2: standard errorint fd; /* file descriptor */if ((fd = open("/etc/hosts", O_RDONLY)) < 0) {perror("open");exit(1);}–6–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.Note: Always check return codes, even for seemingly Note: 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);}Page 2–7–15-213, F’07Reading FilesReading FilesReading a file copies bytes from the current file Reading a file copies bytes from the current file position 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 fdfdinto into bufbuf Return type ssize_t is signed integer nbytes < 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);}–8–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 bufbufto file to file fdfd.. nbytes < 0 indicates that an error occurred. As with reads, short counts are possible and are not errors!Transfers Transfers up toup to512 bytes from address 512 bytes from address bufbufto 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);}–9–15-213, F’07Unix I/O ExampleUnix I/O ExampleCopying standard input to standard output one byte at a Copying standard input to standard output one byte at a time.time.Note the use of error handling wrappers for read and Note the use of error handling wrappers for read and write (Appendix B).write (Appendix B).#include "csapp.h"int main(void) {char c;while(Read(STDIN_FILENO, &c, 1) != 0) Write(STDOUT_FILENO, &c, 1);exit(0);}–10–15-213, F’07Dealing with Short CountsDealing with Short CountsShort counts can occur in these situations:Short counts can occur in these situations: Encountering (end-of-file) EOF on reads. Reading text lines from a terminal. Reading and writing network sockets or Unix pipes.Short counts never occur in these situations:Short counts never occur in these situations: Reading from disk files (except for EOF) Writing to disk files.One way to deal with short counts in your code:One way to deal with short counts in your code: Use the RIO (Robust I/O) package from your textbook’s csapp.c file (Appendix B)–11–15-213, F’07The RIO PackageThe RIO PackageRIO is a set of wrappers that provide efficient and robust I/O iRIO is a set of wrappers that provide efficient and robust I/O in n applications such as network programs that are subject to short applications such as network programs that are subject to short counts.counts.RIO provides two different kinds of functionsRIO provides two different kinds of functions Unbuffered input and output of binary dataz rio_readn and rio_writen Buffered input of binary data and text linesz rio_readlineb and rio_readnbz Buffered RIO routines are thread-safe and can be interleaved arbitrarily on the same descriptor.Download from Download from
View Full Document