Unix I O Key Characteristics 15 213 The course that gives CMU its Zip System Level I O April 1 2008 Classic Unix Linux I O Mainframe I O I O operates on linear streams of Bytes I O operates on structured records I O tends to be synchronous Topics Unix I O Robust reading and writing Reading file metadata Sharing files I O redirection Standard I O Binary data files I O in Java Read or write operation block until data has been transferred Fine grained I O One key stroke at a time Each I O event is handled by the kernel and an appropriate process Functions to locate insert remove update records I O tends to be asynchronous Overlap I O and computation within a process Coarse 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 15 213 S 08 2 class20 ppt Unix File Types Unix Files Regular file A Unix file is a sequence of m bytes Can reposition insertion point and extend file at end B0 B1 Bk Bm 1 All I O devices are represented as files dev sda2 usr disk partition dev tty2 terminal Binary or text file Unix does not know the difference z But some library functions only work for text files Directory file Even the kernel is represented as a file dev kmem kernel memory image proc A file that contains the names and locations of other files Character special and block special files kernel data structures Terminals character special and disks block special FIFO named pipe A file type used for interprocess communication Socket 3 15 213 S 08 4 A file type used for network communication between processes 15 213 S 08 Unix I O Opening Files Key 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 Model B0 B1 E g reading disk files Have read k bytes Bk 1 Bk Bk 1 int fd 5 if fd open etc hosts O RDONLY 0 perror open exit 1 Returns a small identifying integer file descriptor Opening and closing files Changing the current file position seek z lseek not discussed Reading and writing a file z read and write fd 1 indicates that an error occurred Each process created by a Unix shell begins life with three open files associated with a terminal z open and close file descriptor Current File Position k Basic Unix I O operations system calls Opening a file informs the kernel that you are getting ready to access that file 15 213 S 08 6 0 standard input 1 standard output 2 standard error 15 213 S 08 Closing Files Reading Files Closing a file informs the kernel that you are finished accessing that file Reading a file copies bytes from the current file position to memory and then updates file position char buf 512 int fd file descriptor int nbytes number of bytes read int fd file descriptor int retval return value if retval close fd 0 perror close exit 1 Closing an already closed file is a recipe for disaster in threaded programs more on this later Moral Always check return codes even for seemingly benign functions such as close Open file fd Then read up to 512 bytes from file fd if nbytes read fd buf sizeof buf 0 perror read exit 1 Returns number of bytes read from file fd into buf 7 15 213 S 08 8 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 15 213 S 08 Writing Files Unix I O Example Writing a file copies bytes from memory to the current file position and then updates current file position char buf 512 int fd file descriptor int nbytes number of bytes read Copying standard input to standard output one byte at a time This is extremely inefficient include csapp h 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 int main void char c while Read STDIN FILENO c 1 0 Write STDOUT FILENO c 1 exit 0 Returns number of bytes written from buf to file fd fd nbytes 0 indicates that an error occurred Transfers up to 512 bytes from address buf to file fd As with reads short counts are possible and are not errors 15 213 S 08 9 Note the use of error handling wrappers for read and write Appendix B 15 213 S 08 10 Dealing with Short Counts The RIO Package Short counts can occur in these situations RIO is a set of wrappers that provide efficient and robust I O in in applications such as network programs that are subject to short counts Encountering EOF end of file on reads Reading text lines from a terminal Reading and writing network sockets or Unix pipes RIO provides two different kinds of functions Reading from disk files except for EOF Writing to disk files z Buffered RIO routines are thread safe and can be interleaved on the same Use the RIO Robust I O package from your textbook s csapp c file Appendix B z To be discussed 11 Buffered input of binary data and text lines z rio readlineb and rio readnb descriptor How should you deal with short counts in your code Unbuffered input and output of binary data z rio readn and rio writen Short counts never occur in these situations 15 213 S 08 Download from csapp cs cmu edu public ics code src csapp c csapp cs cmu edu public ics code include csapp h 12 15 213 S 08 Unbuffered RIO Input and Output Implementation of rio readn rio readn robustly read n bytes unbuffered ssize t rio readn int fd void usrbuf size t n size t nleft n ssize t nread char bufp usrbuf Same interface as Unix read and write Especially useful for transferring data on network sockets include csapp h while nleft 0 if nread read fd bufp nleft 0 if errno EINTR interrupted by sig handler return nread 0 and call read again else return 1 errno set by read else if nread 0 break EOF nleft nread bufp nread return n nleft return 0 ssize t rio readn int fd void usrbuf size t n ssize t rio writen int fd void usrbuf size t n Return num bytes transferred if OK 0 on EOF rio readn only 1 on error rio readn returns short count only it encounters EOF rio writen never returns a short count Calls to rio readn and rio writen can be interleaved arbitrarily on the same descriptor 15 213 S 08 z Only use it when you know how many bytes to read 13 Buffered I O Motivation Buffered I O Implementation I O Applications Read Write One Character at a Time getc …
View Full Document