Announcements 15 213 Final exam day time announced by CMU The course that gives CMU its Zip System Level I O October 9 2008 8 30 11 30am on Friday December 12 Cheating Cheating please please don don t Writing code together counts as sharing code forbidden Pair programming even w o looking at other s code forbidden Opening up code and then leaving it for someone to enjoy forbidden Talking through a problem can include pictures not code ok The automated tools for discovering cheating are incredibly good Everyone has been warned multiple times z describing code line by line counts the same as sharing code Topics z in fact please remember to use protected directories and screen locking Unix I O Robust reading and writing Reading file metadata Sharing files I O redirection Standard I O z please don t test them z cheating on the remaining labs will receive no mercy 15 213 F 08 2 lecture 13 ppt Unix Files Unix File Types A Unix file is a sequence of m bytes Regular file B0 B1 Bk Bm 1 All I O devices are represented as files dev sda2 usr disk partition dev tty2 terminal z other than sequence of bytes akin to main memory Directory file Even the kernel is represented as a file File containing user app data binary text whatever OS does not know anything about the format A file that contains the names and locations of other files Character special and block special files dev kmem kernel memory image proc kernel data structures Terminals character special and disks block special FIFO named pipe A file type used for inter process communication Socket 3 15 213 F 08 4 Page 1 A file type used for network comm between processes 15 213 F 08 Unix I O Opening Files Key Features Opening a file informs the kernel that you are getting ready to access that file 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 int fd if fd open etc hosts O RDONLY 0 perror open exit 1 Basic Unix I O operations system calls Opening and closing files Reading and writing a file file descriptor z open and close Returns a small identifying integer file descriptor z read and write Changing the current file position seek Each process created by a Unix shell begins life with three open files associated with a terminal z indicates next offset into file to read or write z Lseek B0 B1 Bk 1 Bk Bk 1 5 fd 1 indicates that an error occurred Current File Position k 6 15 213 F 08 0 standard input 1 standard output 2 standard error 15 213 F 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 Open file fd Then read up to 512 bytes from file fd if nbytes read fd buf sizeof buf 0 perror read exit 1 Closing an already closed file is a recipe for disaster in threaded programs more on this later Returns number of bytes read from file fd into buf Moral Always check return codes even for seemingly benign functions such as close 7 15 213 F 08 8 Page 2 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 Simple Unix I O example Writing a file copies bytes from memory to the current file position and then updates current file position Copying standard in to standard out one byte at a time char buf 512 int fd file descriptor int nbytes number of bytes read int main void char c int len 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 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 Returns number of bytes written from buf to file fd nbytes 0 indicates that an error occurred As with reads short counts are possible and are not errors 15 213 F 08 9 File Metadata Example of Accessing File Metadata statcheck c Querying and manipulating a file s meta data include csapp h Metadata is data about data in this case file data int main int argc char argv struct stat stat char type readok PerPer file metadata maintained by kernel 15 213 F 08 10 accessed by users with the stat and fstat functions 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 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 15 213 F 08 11 printf type s read s n type readok exit 0 12 Page 3 15 213 F 08 How the Unix Kernel Represents Open Files Repeated slide Opening Files Opening a file informs the kernel that you are getting ready to access that file int fd Two descriptors referencing two distinct open disk files Descriptor 1 stdout stdout points to terminal and descriptor 4 points to open disk file file descriptor if fd open etc hosts O RDONLY 0 perror open exit 1 Descriptor table one table per process File B disk File pos refcnt 1 14 File access File size File type 15 213 F 08 File Sharing How Processes Share Files Two distinct descriptors sharing the same disk file through two distinct open file table entries A child process inherits its parent parent s open files Open file table shared by all processes Before fork call v node table shared by all processes File A Open file table shared by all processes Parent s table File A …
View Full Document