System-Level I/OOctober 9, 2008System-Level I/OOctober 9, 2008TopicsTopics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O15-213“The course that gives CMU its Zip!”lecture-13.ppt–2–15-213, F’08AnnouncementsFinal exam day/time announced (by CMU)Final exam day/time announced (by CMU) 8:30-11:30am on Friday, December 12CheatingCheating……please, please donplease, please don’’tt Writing code together counts as “sharing code” – forbidden “Pair programming”, even w/o looking at other’s code – forbiddenz describing code line by line counts the same as sharing code Opening up code and then leaving it for someone to enjoy – forbiddenz 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 goodz … please don’t test them Everyone has been warned multiple timesz cheating on the remaining labs will receive no mercy–3–15-213, F’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, F’08Unix File TypesUnix File TypesRegular fileRegular file File containing user/app data (binary, text, whatever) OS does not know anything about the formatz other than “sequence of bytes”, akin to main memoryDirectory fileDirectory file A file that contains the names and locations of other filesCharacter 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 inter-process communicationSocketSocket A file type used for network comm. between processes–5–15-213, F’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.Basic Unix I/O operations (system calls): Basic Unix I/O operations (system calls): Opening and closing filesz open()and close() Reading and writing a filez read() and write() Changing the current file position (seek)z indicates next offset into file to read or writez Lseek ()B0B1••• Bk-1BkBk+1•••Current File Position = k–6–15-213, F’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 fileready to access that fileReturns 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, F’08Closing FilesClosing FilesClosing a file informs the kernel that you are finished Closing a file informs the kernel that you are finished accessing that fileaccessing that fileClosing 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, F’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 positionposition to memory, and then updates file positionReturns 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, F’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 positionposition, and then updates current file positionReturns number of bytes written from Returns number of bytes written from bufbufto file to file fdfd nbytes < 0 indicates that an error occurred As with reads, short counts are possible and are not errors!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);}–10–15-213, F’08Simple Unix I/O exampleSimple Unix I/O exampleCopying standard in to standard out, one byte at a timeCopying standard in to standard out, one byte at a timeint main(void) {char c;int len;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);}–11–15-213, F’08File MetadataFile MetadataMetadataMetadatais data about data, in this case file datais data about data, in this case file dataPerPer--file metadata maintained by kernelfile 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; /*
View Full Document