System-Level I/O April 1, 2008Unix 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 I/O: DeclarationBuffered RIO Input FunctionsBuffered RIO Input Functions (cont)RIO 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/OBuffering in Standard I/OBuffering in Standard I/OUnix I/O vs. Standard I/O vs. RIOPros and Cons of Unix I/OPros and Cons of Standard I/OChoosing I/O FunctionsFor Further InformationWorking with Binary FilesJava I/OSystem-Level I/OApril 1, 2008System-Level I/OApril 1, 2008TopicsTopics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O Binary data files I/O in Java15-213“The course that gives CMU its Zip!”class20.ppt–2–15-213, S’08Unix 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 Bytes Can reposition insertion point and extend file at endI/O tends to be synchronousI/O tends to be synchronous Read or write operation block until data has been transferredFine grained I/OFine grained I/O One key-stroke at a time Each 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 recordsrecords Functions to locate, insert, remove, update recordsI/O tends to be asynchronousI/O tends to be asynchronous Overlap I/O and computation within a processCoarse grained I/OCoarse grained I/O Process writes “channel programs” to be executed by the I/O hardware Many I/O operations are performed autonomously with one interrupt at completion–3–15-213, S’08Unix 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)–4–15-213, S’08Unix File TypesUnix File TypesRegular fileRegular file Binary or text file. Unix does not know the difference!z But some library functions only work for text filesDirectory 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–5–15-213, S’08Unix I/OUnix I/OKey FeaturesKey Features Elegant mapping of files to devices allows kernel to export simple interface called Unix I/O. Important idea: All input and output is handled in a consistent and uniform way.Typical File ModelTypical File Model E.g., reading disk files Have read k bytesBasic 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()B0B1••• Bk-1BkBk+1•••Current File Position = k–6–15-213, S’08Opening 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);}–7–15-213, S’08Closing 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, S’08Reading 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);}–9–15-213, S’08Writing 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.Transfers Transfers up toup to512 bytes from address 512
View Full Document