CS241 System Programming File System IIIContentAdministrativeReading and WritingSequential FilesStandard filesSequential write operationCopyfileSlide 9Use 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/200601/14/19 CS 241 - System Programming, Klara Nahrstedt2Content Reading and Writing in UNIXOpening and Closing FilesSelect Function File Representation in UNIXSummary01/14/19 CS 241 - System Programming, Klara Nahrstedt3Administrative R: Ch 4 pp92-135MP3 is posted, due April 3, 2006Quiz 6 is March 17, 2006–Topics covered for Quiz 6: Tanenbaum: 3.1-3.3 (I/O and Deadlock)Tanenbaum: 5.1-5.2 (File and Directories)R&R: 4.1., 4.2, 4.3, 4.4, 4.6.1, 4.6.2Lecture Notes up to Monday (March 13)01/14/19 CS 241 - System Programming, Klara Nahrstedt4Reading and Writing Sequential access to files and other devices give by read and write functionsA 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 file01/14/19 CS 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 file01/14/19 CS 241 - System Programming, Klara Nahrstedt6Standard filesstdin – standard input, file descriptor STDIN_FILENO (legacy code standard fd = 0) stdout – standard output, file descriptor STDOUT_FILENO (fd = 1)stderr – standard output, file descriptor STDERR_FILENO (fd = 2)Use always symbolic names rather than the numeric values01/14/19 CS 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?01/14/19 CS 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;01/14/19 CS 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;}01/14/19 CS 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;}01/14/19 CS 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;}01/14/19 CS 241 - System Programming, Klara Nahrstedt12Use ’readblock’ function 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 errnoUse it to read structuresEither works or returns error01/14/19 CS 241 - System Programming, Klara Nahrstedt13Opening and Closing FilesOpen associates a file descriptor with a file or physical deviceFile descriptor is an index into a file descriptor table per process and can be inherited to child processes allowing sharingOne should close open filesOflag access modes and status#include <fcntl.h>#include <sys/stat.h>int open(const char *path, int oflag,..)01/14/19 CS 241 - System Programming, Klara Nahrstedt14UNIX file structure implementationFile positionR/WPointer to inodeFile positionR/WPointer to inodeModeLink CountUIDGIDFile sizeTimesAddress of first 10 disk blocksSingle IndirectDouble IndirectTriple IndirectinodeOpen file descriptionParent File descriptortableChildFile descriptortableUnrelated processFile descriptor table01/14/19 CS 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.01/14/19 CS 241 - System Programming, Klara Nahrstedt16Additional OflagsO_APPEND – file offset to EOFO_CREAT – need to give permissionsO_EXCL – use with CREATE detects fileO_NOCTTY – prevents device from becoming controlling terminalO_NONBLOCK – return immediatelyO_TRUNC – file to beginning for write01/14/19 CS 241 - System
View Full Document