DOC PREVIEW
U of I CS 241 - Lecture

This preview shows page 1-2-3-4-25-26-27-52-53-54-55 out of 55 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 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 55 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 55 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 55 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 55 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 55 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 55 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 55 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 55 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 55 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 55 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Unix File StructureInternal File StructureSlide 8I/O Libraries in CSlide 10Buffered I/O AdvantagesFile Descriptorsopen()Slide 14fopen()File PermissionsSlide 17Slide 18Slide 19Slide 20Slide 21Slide 22Other C file commands!Slide 24Buffered I/O versions…Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55CS 241 Section Week #8(10/29/09)Outline•MP5 Overview•Files & I/O•UNIX File Systems–inodes–Directories–LinksMP5 OverviewMP5 Overview•You are to create a “deadlock resilient semaphore” library. You should implement six functions.•Since we only allow one instance of each resource, you do not need to implement the Banker’s algorithm for deadlock prevention. You may use a resource allocation graph instead.•In deadlock detection mode, once a deadlock is detected, you need only send a SIGINT signal. The library does NOT need to worry about how SIGINT is handled.•The given test cases are far from complete. You should derive your own test cases.Files and I/OUnix File Structure/ /bin/ /home/ /home/someuser/ /home/someuser/somefile.txt /usr/ /usr/bin/ /usr/lib/Internal File Structure•A file is just a series of bytes:T h i s w n i x f i l e .Internal File Structure•A file is just a series of bytes:T h i s w n i x f i l e .Start of File End of FileCurrent OffsetI/O Libraries in Ckernel system call handlerfileI/OterminalI/OpipeI/OnetworkI/OaudioI/Oopen, read, write, close, select, poll, ...(direct to kernel I/O)stdio: fopen, fread, fwrite, fclose, ...(buffered I/O)User ProcessKernelI/O Libraries in Ckernel system call handlerfileI/OterminalI/OpipeI/OnetworkI/OaudioI/Oopen, read, write, close, select, poll, ...(direct to kernel I/O)stdio: fopen, fread, fwrite, fclose, ...(buffered I/O)User ProcessKernelBuffered I/O Advantages•We’ve previously used:–printf(…)–fprintf(…)•Why use buffers?–I/O operations are SLOW!–Every time you write just one byte, you don’t want to have to access your hard drive.File Descriptors•The UNIX operating system uses a file descriptor table to store information about open files:0 stdin … (sof, eof, offset values, etc)1 stdout …2 stderr …36 /usr/home/myfile.txt …open()•int open(const char *pathname, int flags);–returns an int, which is the file descriptor–takes in either a relative or full path name–various flag options allow a file to only be appended to (O_APPEND), opened as write only (O_WRONLY), and more.open()•To open a file for reading:int ifd = open(“./input.txt”, O_RDONLY);•To open OR create a file for writing, with given permissions:–int ofd = open(“output.txt”, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);fopen()•FILE *fopen( const char *filename, const char *mode);•Rather than an int (file descriptor), fopen returns a FILE stream.File Permissions•In UNIX, the file permissions system is relatively basic.–Each file has a single owner and a single group associated with it.–Each file also has permissions associated with itself for the owner, members of the group the file is in, and for everyone else.File Permissions•These permissions are stored as a three-octal-digit number (000 to 777).7 5 5File Permissions•The most-significant number is the owner’s permission.7 5 5OwnerFile Permissions•The middle number is the group’s permission.7 5 5GroupFile Permissions•The least-significant number is everyone else’s permission.7 5 5OtherFile Permissions•Each octal number is simply three bits: a read bit, a write bit, and an execute bit.7 5 511111110 0Read:Write:Execute:File Permissions•Thus:–755 means “everyone can read and execute by file, but only the owner can write to (edit) my file”–644 means “everyone can read my file, only the owner can write to my file, and no one can execute it”–660 means “only members of the file’s group and the file’s owner may read or edit the file; others cannot even read it”Other C file commands!•close(int fd)–Close the file associated with the given file descriptor number.–Can you close stdout? Try it.•fclose(FILE *stream)–fclose can close stdout.Other C file commands!•ssize_t read(int fd, void *buf, size_t count);–Read up to count bytes from a file descriptor into the buffer buf.•ssize_t write(int fd, void *buf, size_t count);–Write count bytes to a file descriptor from the buffer buf.Buffered I/O versions…•size_t fread(void *ptr, size_t size, size_t count, FILE* stream);–Read up to count*size bytes from a file descriptor into the buffer ptr.•size_t fwrite(void *ptr, size_t size, size_t count, FILE* stream);–Write count*size bytes to a file descriptor from the buffer ptr.Other C file commands!•off_t lseek(int fd, off_t offset, int whence);–Seek to a different point in the file.–lseek(fd, 4, SEEK_SET)•Seek four bytes after the beginning of the file.–lseek(fd, -4, SEEK_END)•Seek four bytes before the end of the file.–lseek(fd, 16, SEEK_CUR)•Seek sixteen bytes ahead of the current position.Other C file commands!•int fseek(FILE *stream, long int offset, int origin); –fseek(stream, 4, SEEK_SET)•Seek four bytes after the beginning of the file.–fseek(stream, -4, SEEK_END)•Seek four bytes before the end of the file.–fseek(stream, 16, SEEK_CUR)•Seek sixteen bytes ahead of the current position.UNIX File SystemsUNIX File Systemsinode: per-file data structureAdvantageEfficient for small filesFlexible if the size changesDisadvantageFile must fit in a single disk partitionUNIX File Systemsinode (continued)Storing Large FilesDirectories are files too!•Directories, like files, have inodes with attributes and pointers to disk blocksDirectories are files too!•Directories, like files, have inodes with attributes and pointers to disk blocks•Each directory contains the name and i-node for each file in the directory.Directories are files too!•Directories, like files, have inodes with attributes and pointers to disk blocks•Each directory contains the name and i-node for each file in the directory.Directory functions#include <unistd.h>Change the directoryint chdir(const char *path);Get the current working directorychar *getcwd(char *buf,


View Full Document

U of I CS 241 - Lecture

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

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 Lecture
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 Lecture 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 Lecture 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?