Unformatted text preview:

CS 105 Tour of the Black Holes of Computing Input and Output Topics I O hardware Unix file abstraction Robust I O File sharing I O A Typical Hardware System CPU chip register file ALU system bus memory bus main memory I O bridge bus interface I O bus USB controller mouse keyboard 2 graphics adapter disk controller Expansion slots for other devices such as network adapters monitor disk CS 105 Abstracting I O Low level requires complex device commands Vary from device to device Device models can be very different Tape read or write sequentially or rewind Disk random access at block level Terminal sequential no rewind must echo and allow editing Video write only with 2 dimensional structure Operating system should hide these differences 3 Read and write should work regardless of device Sometimes impossible to generalize e g video Still need access to full power of hardware CS 105 Unix Files A Unix file is a sequence of m bytes B0 B1 Bk Bm 1 All I O devices are represented as files dev sda2 usr disk partition dev tty2 terminal Even the kernel is represented as a file 4 dev kmem kernel memory image proc kernel data structures CS 105 Unix File Types Regular file binary or text Unix does not know the difference Directory file contains the names and locations of other files Character special file keyboard and network for example Block special file like disks FIFO named pipe used for interprocess comunication Socket used for network communication between processes 5 CS 105 Unix I O The elegant mapping of files to devices allows kernel to export simple interface called Unix I O Key Unix idea All input and output is handled in a consistent and uniform way Basic Unix I O operations system calls 6 Opening and closing files open and close Changing the current file position seek llseek not discussed Reading and writing a file read and write CS 105 Opening Files int fd file descriptor if fd open etc hosts O RDONLY 1 fprintf stderr Couldn t open etc hosts s strerror errno exit 1 Opening a file informs kernel that you are getting ready to access that file Returns a small identifying integer file descriptor fd 1 indicates that an error occurred errno has reason strerror converts to English Note use strerror r for thread safety Each process created by a Unix shell begins life with three open files normally connected to terminal 7 0 standard input 1 standard output 2 standard error CS 105 Closing Files int fd file descriptor int retval return value if retval close fd 1 perror close exit 1 Closing a file tells kernel that you re finished with it Closing an already closed file is recipe for disaster in threaded programs more on this later Some error reports are delayed until close Moral Always check return codes even for seemingly benign functions such as close perror is simplified strerror fprintf see man page 8 CS 105 Reading Files char buf 4096 int fd unsigned int nbytes file descriptor number of bytes read Open file fd Then read up to 4096 bytes from file fd if nbytes read fd buf sizeof buf 1 perror read exit 1 Reading a file copies bytes from current file position to memory then updates file position Returns number of bytes read from file fd into buf 9 nbytes 1 indicates error occurred 0 indicates end of file EOF short counts nbytes sizeof buf are possible and are not errors CS 105 Writing Files char buf 4096 int fd unsigned int nbytes file descriptor number of bytes read Open the file fd Then write up to 4096 bytes from buf to file fd if nbytes write fd buf sizeof buf 1 perror write exit 1 Writing a file copies bytes from memory to current file position then updates current file position Returns number of bytes written from buf to file fd nbytes 1 indicates that an error occurred As with reads short counts are possible and are not errors Here transfers up to 4096 bytes from address buf to file fd 10 CS 105 Simple Example include csapp h int main void char c while Read STDIN FILENO c 1 0 Write STDOUT FILENO c 1 exit 0 Copying standard input to standard output one byte at a time Note the use of error handling wrappers for read and write Appendix B in text 11 CS 105 Dealing with Short Counts 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 Reading from disk files except for EOF Writing to disk files How should you deal with short counts in your code Use the RIO Robust I O package from your textbook s csapp c file Appendix B But note that it handles EOF wrong on terminal 12 Use C stdio or C streams Write your code very very carefully Ignore the problem and accept that your code is fragile CS 105 Foolproof I O Low level I O is difficult because of short counts and other possible errors Textbook provides RIO package a fairly good example of how to encapsulate low level I O RIO is set of wrappers that provide efficient and robust I O in applications e g network programs that are subject to short counts Download from csapp cs cmu edu public ics code src csapp c csapp cs cmu edu public ics code include csapp h 13 CS 105 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 14 while nleft 0 if nread read fd bufp nleft 1 if errno EINTR interrupted by signal 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 CS 105 Unbuffered I O RIO provides buffered and unbuffered routines Unbuffered Especially useful for transferring data on network sockets Same interface as Unix read and write rio readn returns short count only it encounters EOF Usually incorrect if reading from terminal 15 rio writen never returns a short count Calls to rio readn and rio writen can be interleaved arbitrarily on the same descriptor CS 105 Buffered Input Buffered Efficiently read text lines and binary data from file partially cached in an internal memory buffer rio readlineb reads text line of up to maxlen bytes from file fd and stores it in usrbuf Especially useful for reading lines from network sockets rio readnb reads up to n bytes from file fd Calls to rio readlineb and rio readnb can be interleaved arbitrarily on same descriptor Warning Don t interleave with calls to rio readn with calls to b versions 16 CS 105 Buffered Example Copying the lines of a text file from


View Full Document

Harvey Mudd CS 105 - Input and Output

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Loading Unlocking...
Login

Join to view Input and Output and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Input and Output and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?