15 213 The course that gives CMU its Zip System Level I O October 9 2008 Topics lecture 13 ppt Unix I O Robust reading and writing Reading file metadata Sharing files I O redirection Standard I O Announcements Final exam day time announced by CMU 8 30 11 30am on Friday December 12 Cheating please please don t Writing code together counts as sharing code forbidden Pair programming even w o looking at other s code forbidden describing code line by line counts the same as sharing code Opening up code and then leaving it for someone to enjoy forbidden in fact please remember to use protected directories and screen locking Talking through a problem can include pictures not code ok The automated tools for discovering cheating are incredibly good please don t test them Everyone has been warned multiple times cheating on the remaining labs will receive no mercy 2 15 213 F 08 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 3 dev kmem kernel memory image proc kernel data structures 15 213 F 08 Unix File Types Regular file File containing user app data binary text whatever OS does not know anything about the format other than sequence of bytes akin to main memory Directory file A file that contains the names and locations of other files Character special and block special files Terminals character special and disks block special FIFO named pipe A file type used for inter process communication Socket 4 A file type used for network comm between processes 15 213 F 08 Unix I O 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 Basic Unix I O operations system calls Opening and closing files open and close Reading and writing a file read and write Changing the current file position seek indicates next offset into file to read or write Lseek B0 B1 5 Bk 1 Bk Bk 1 Current File Position k 15 213 F 08 Opening Files Opening a file informs the kernel that you are getting ready to access that file int fd file descriptor if fd open etc hosts O RDONLY 0 perror open exit 1 Returns a small identifying integer file descriptor fd 1 indicates that an error occurred Each process created by a Unix shell begins life with three open files associated with a terminal 6 0 standard input 1 standard output 2 standard error 15 213 F 08 Closing Files Closing a file informs the kernel that you are finished accessing that file 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 7 15 213 F 08 Reading Files 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 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 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 F 08 Writing Files 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 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 Returns number of bytes written from buf to file fd 9 nbytes 0 indicates that an error occurred As with reads short counts are possible and are not errors 15 213 F 08 Simple Unix I O example Copying standard in to standard out one byte at a time int main void char c int len 10 while len read 0 stdin c 1 1 if write 1 stdout c 1 1 exit 20 if len 0 printf read from stdin failed exit 10 exit 0 15 213 F 08 File Metadata Metadata is data about data in this case file data Per file metadata maintained by kernel accessed by users with the stat and fstat functions Metadata returned by the stat and fstat functions struct stat dev t st dev device ino t st ino inode mode t st mode protection and file type nlink t st nlink number of hard links uid t st uid user ID of owner gid t st gid group ID of owner dev t st rdev device type if inode device off t st size total size in bytes unsigned long st blksize blocksize for filesystem I O unsigned long st blocks number of blocks allocated time t st atime time of last access time t st mtime time of last modification time t st ctime time of last change 11 15 213 F 08 Example of Accessing File Metadata statcheck c Querying and manipulating a file s meta data include csapp h int main int argc char argv struct stat stat char type readok unix type unix unix type unix type unix type statcheck statcheck c regular read yes chmod 000 statcheck c statcheck statcheck c regular read no statcheck directory read yes statcheck dev kmem other read yes Stat argv 1 stat if S ISREG stat st mode type regular else if S ISDIR stat st mode type directory else type other if stat st mode S IRUSR OK to read readok yes else readok no printf type s read s n type readok exit 0 12 15 213 F 08 Repeated slide Opening Files Opening a file informs the kernel that you are getting ready to access that file int fd file descriptor if fd open etc hosts O RDONLY 0 perror open exit 1 Returns a small identifying integer file descriptor fd 1 indicates that an error occurred Each process created by a Unix shell begins life with three open files associated with a terminal 13 0 standard input stdin 1 standard output stdout 2 standard error stderr 15 213 F 08 How the Unix Kernel Represents Open Files Two descriptors referencing two distinct open disk files Descriptor 1 stdout points to terminal and descriptor 4 points to open disk file Descriptor table one table per process File A terminal File pos refcnt 1 File B disk File pos 14 File access File size File type Info in stat struct File access File size File type refcnt 1 v node table shared by all processes stdin fd 0 stdout fd 1 stderr fd 2 fd 3 fd …
View Full Document