CS241 System ProgrammingFile System IIIContentAdministrativeReading and WritingSequential FilesStandard filesSequential write operationCopyfileCopyfileUse the restart library for read/write P99 – e.g. r_writeRestart functions make programs simpler!Use ’readblock’ functionOpening and Closing FilesUNIX file structure implementationFile Access ExampleAdditional OflagsAccess Control sys/stat.hCloseSELECT FunctionSelect Function (2)File RepresentationFile DescriptorsFile Pointers and bufferingSchematic Handling of a file pointer after fopenProblems with FILE pointersSummaryCS241 System ProgrammingFile System IIIKlara NahrstedtLecture 223/13/20063/12/2006CS 241 - System Programming, Klara Nahrstedt2Content z Reading and Writing in UNIXz Opening and Closing Filesz Select Function z File Representation in UNIXz Summary3/12/2006CS 241 - System Programming, Klara Nahrstedt3Administrative z R: Ch 4 pp92-135z MP3 is posted, due April 3, 2006z Quiz 6 is March 17, 2006– Topics covered for Quiz 6: z Tanenbaum: 3.1-3.3 (I/O and Deadlock)z Tanenbaum: 5.1-5.2 (File and Directories)z R&R: 4.1., 4.2, 4.3, 4.4, 4.6.1, 4.6.2z Lecture Notes up to Monday (March 13)3/12/2006CS 241 - System Programming, Klara Nahrstedt4Reading and Writing z Sequential access to files and other devices give by read and write functionsz A read operation – Attempts to retrieve nbytes from the file or device fildes into the user variable buf– May return fewer bytes than requested if, e.g. it reaches end-of-file before completely satisfying the request. – Returns 0 to indicate end-of-file for a regular file3/12/2006CS 241 - System Programming, Klara Nahrstedt5Sequential Files#include <unitstd.h>ssize_t read(int fildes, void *buf, size_t nbyte);•Returns number of bytes read in buf•*buf must be initialized.•read on pipe may return < nbytes•pipe – simplest UNIX inter-process communication mechanism, represented by a special file3/12/2006CS 241 - System Programming, Klara Nahrstedt6Standard filesz stdin – standard input, file descriptor STDIN_FILENO (legacy code standard fd = 0)z stdout – standard output, file descriptor STDOUT_FILENO (fd = 1)z stderr – standard output, file descriptor STDERR_FILENO (fd = 2)z Use always symbolic names rather than the numeric values3/12/2006CS 241 - System Programming, Klara Nahrstedt7Sequential write operation#include <unistd.h>ssize_t write(int fildes, const void *buf, size_t nbyte);•Returns number of bytes actually written•Can only write to end of file (or file truncates)#define BLKSIZE 1024char buf[BLKSIZE}; read(STDIN_FILENO, buf, BLKSIZE);write(STDOUT_FILENO, buf, BLKSIZE);What can go wrong with the above code segment?3/12/2006CS 241 - System Programming, Klara Nahrstedt8Copyfile#include <errno.h> #include <unistd.h>#define BLKSIZE 1024int copyfile(int fromfd, int tofd) {char *bp;char buf[BLKSIZE]; int bytesread;int byteswritten = 0;int totalbytes = 0;for ( ; ; ) {while (((bytesread = read(fromfd, buf, BLKSIZE)) == -1) && (errno == EINTR)) ; /* handle interruption by signal */if (bytesread < 0) /* real error or end-of-file on fromfd */break; bp = buf;3/12/2006CS 241 - System Programming, Klara Nahrstedt9Copyfilewhile (bytesread > 0) { while(((byteswritten = write(tofd, bp, bytesread)) == -1 ) &&(errno == EINTR)) ; /* handle interruption by signal */if (byteswritten <= 0) /* real error on tofd */break;totalbytes += byteswritten;bytesread -= byteswritten;bp += byteswritten; }if (byteswritten == -1) /* real error on tofd */break;}return totalbytes;}3/12/2006CS 241 - System Programming, Klara Nahrstedt10Use the restart library for read/write P99 – e.g. r_writessize_t r_write(int fd, void *buf, size_t size) {char *bufp;size_t bytestowrite;ssize_t byteswritten;size_t totalbytes;for (bufp = buf, bytestowrite = size, totalbytes = 0;bytestowrite > 0;bufp += byteswritten, bytestowrite -= byteswritten) {byteswritten = write(fd, bufp, bytestowrite);if ((byteswritten) == -1 && (errno != EINTR))return -1;if (byteswritten == -1)byteswritten = 0;totalbytes += byteswritten;}return totalbytes;}3/12/2006CS 241 - System Programming, Klara Nahrstedt11Restart functions make programs simpler!#include <unistd.h>#include "restart.h"#define BLKSIZE 1024int copyfile(int fromfd, int tofd) {char buf[BLKSIZE]; int bytesread, byteswritten;int totalbytes = 0;for ( ; ; ) {if ((bytesread = r_read(fromfd, buf, BLKSIZE)) <= 0)break; if ((byteswritten = r_write(tofd, buf, bytesread)) == -1)break;totalbytes += byteswritten;}return totalbytes;}3/12/2006CS 241 - System Programming, Klara Nahrstedt12Use ’readblock’ function z Read a specific number of bytes– Return 0 if end-of-file occurs before any bytes are read– Return size if ‘readblock’ successful– Return -1 in case of error and sets errnoz Use it to read structuresz Either works or returns error3/12/2006CS 241 - System Programming, Klara Nahrstedt13Opening and Closing Filesz Open associates a file descriptor with a file or physical devicez File descriptor is an index into a file descriptor table per process and can be inherited to child processes allowing sharingz One should close open filesz Oflag access modes and status#include <fcntl.h>#include <sys/stat.h>int open(const char *path, int oflag,..)3/12/2006CS 241 - System Programming, Klara Nahrstedt14UNIX file structure implementationFile positionR/WPointer to inodeFile positionR/WPointer to inodeModeLink CountUIDGIDFile sizeTimesAddress offirst 10 disk blocksSingle IndirectDouble IndirectTriple IndirectinodeOpen file descriptionParent File descriptortableUnrelated processFile descriptor tableChildFile descriptortable3/12/2006CS 241 - System Programming, Klara Nahrstedt15File Access Exampleint fd;mode_t fdmode = (S_IRUSR | S_IWUSR |S_IRGRP | S_IROTH);If ((fd = open(“info.dat”, O_RDWR | O_CREAT, fdmode)) == -1) perror(“Failed to open info.dat”);Opens a file info.dat in current directory rewriting any existing file data if present.3/12/2006CS 241 - System Programming, Klara Nahrstedt16Additional Oflagsz O_APPEND – file offset to EOFz O_CREAT – need to give permissionsz O_EXCL – use with CREATE detects filez O_NOCTTY – prevents device from becoming controlling terminalz O_NONBLOCK – return immediatelyz O_TRUNC – file to beginning for write3/12/2006CS 241 - System Programming, Klara Nahrstedt17Access Control sys/stat.hR W X R W X R W Xgroup
View Full Document