System-Level I/O October 9, 2008AnnouncementsUnix FilesUnix File TypesUnix I/OOpening FilesClosing FilesReading FilesWriting FilesSimple Unix I/O exampleFile MetadataExample of Accessing File MetadataRepeated slide: Opening FilesHow the Unix Kernel Represents Open FilesFile SharingHow Processes Share FilesParent and child file sharing after fork()I/O RedirectionI/O Redirection exampleI/O Redirection Example (cont)Standard I/O FunctionsStandard I/O StreamsBuffering in Standard I/OStandard I/O Buffering in ActionFork Example #2 (Original)Fork Example #2 (modified)Repeated slide: Reading FilesDealing with Short CountsThe RIO PackageUnbuffered RIO Input and OutputImplementation of rio_readnBuffered I/O: MotivationBuffered I/O: ImplementationBuffered I/O: DeclarationBuffered RIO Input FunctionsBuffered RIO Input Functions (cont)RIO ExampleChoosing I/O FunctionsFor Further InformationFun with File Descriptors (1)Fun with File Descriptors (2)Fun with File Descriptors (3)Accessing DirectoriesUnix I/O Key CharacteristicsUnix I/O vs. Standard I/O vs. RIOPros and Cons of Unix I/OPros and Cons of Standard I/OWorking with Binary FilesJava I/OSystem-Level I/OOctober 9, 2008System-Level I/OOctober 9, 2008TopicsTopicsUnix I/ORobust reading and writingReading file metadataSharing filesI/O redirectionStandard 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 12Cheating… please, please don’tCheating… please, please don’tWriting code together counts as “sharing code” – forbidden“Pair programming”, even w/o looking at other’s code – forbiddendescribing code line by line counts the same as sharing codeOpening up code and then leaving it for someone to enjoy – forbiddenin fact, please remember to use protected directories and screen lockingTalking through a problem can include pictures (not code) – okThe automated tools for discovering cheating are incredibly good… please don’t test themEveryone has been warned multiple timescheating on the remaining labs will receive no mercy– 3 –15-213, F’08Unix FilesUnix FilesA Unix A Unix filefile is a sequence of is a sequence of mm bytes: 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 fileFile containing user/app data (binary, text, whatever)OS does not know anything about the formatother than “sequence of bytes”, akin to main memoryDirectory fileDirectory fileA file that contains the names and locations of other filesCharacter special and block special filesCharacter special and block special filesTerminals (character special) and disks ( block special)FIFO (named pipe)FIFO (named pipe)A file type used for inter-process communicationSocketSocketA file type used for network comm. between processes– 5 –15-213, F’08Unix I/OUnix I/OKey FeaturesKey FeaturesElegant 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 filesopen()and close()Reading and writing a fileread() and write()Changing the current file position (seek)indicates next offset into file to read or writeLseek ()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 descriptorfd == -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 input1: standard output2: 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 position to Reading a file copies bytes from the current file position to memory, and then updates file positionmemory, and then updates file positionReturns number of bytes read from file Returns number of bytes read from file fdfd into into bufbufReturn type ssize_t is signed integernbytes < 0 indicates that an error occurredshort 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 bufbuf to file to file fdfdnbytes < 0 indicates that an error occurredAs with reads, short counts are possible
View Full Document