DOC PREVIEW
U of I CS 241 - System Programming File System III

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 UNIXOpening and Closing FilesSelect Function File Representation in UNIXSummary01/14/19 CS 241 - System Programming, Klara Nahrstedt3Administrative R: Ch 4 pp92-135MP3 is posted, due April 3, 2006Quiz 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.2Lecture 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 functions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 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 filesstdin – 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 errnoUse it to read structuresEither works or returns error01/14/19 CS 241 - System Programming, Klara Nahrstedt13Opening and Closing FilesOpen associates a file descriptor with a file or physical deviceFile descriptor is an index into a file descriptor table per process and can be inherited to child processes allowing sharingOne should close open filesOflag 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 OflagsO_APPEND – file offset to EOFO_CREAT – need to give permissionsO_EXCL – use with CREATE detects fileO_NOCTTY – prevents device from becoming controlling terminalO_NONBLOCK – return immediatelyO_TRUNC – file to beginning for write01/14/19 CS 241 - System


View Full Document

U of I CS 241 - System Programming File System III

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download System Programming File System III
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view System Programming File System III and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view System Programming File System III 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?