DOC PREVIEW
U of I CS 241 - Lecture notes

This preview shows page 1-2-3-4-29-30-31-32-59-60-61-62 out of 62 pages.

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

Unformatted text preview:

CS 241 Section Week 8 4 2 09 Outline LMP1 Overview Files I O UNIX File Systems inodes Directories Links LMP1 Overview LMP1 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 O Unix 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 Start of File i s w Current Offset n i x f i l e End of File I O Libraries in C User Process stdio fopen fread fwrite fclose buffered I O open read write close select poll direct to kernel I O Kernel kernel system call handler file I O terminal I O pipe I O network I O audio I O I O Libraries in C User Process stdio fopen fread fwrite fclose buffered I O open read write close select poll direct to kernel I O Kernel kernel system call handler file I O terminal I O pipe I O network I O audio I O Buffered 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 1 2 stdin stdout stderr 36 usr home myfile txt sof eof offset values etc open int open const char pathname int flags open int open const char pathname int flags open returns an int which is your file descriptor open int open const char pathname int flags open takes in either a relative or full path name usr importantfile txt open int open const char pathname int flags Various flag options to 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 octaldigit number 000 to 777 7 5 5 File Permissions The most significant number is the owner s permission Owner 7 5 5 File Permissions The middle number is the group s permission Group 7 5 5 File Permissions The least significant number is everyone else s permission Other 7 5 5 File Permissions Each octal number is simply three bits a read bit a write bit and an execute bit Read Write Execute 7 5 5 1 1 1 1 0 1 1 0 1 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 Just like close 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 Systems UNIX File Systems inode per file data structure Advantage Efficient for small files Flexible if the size changes Disadvantage File must fit in a single disk partition UNIX File Systems inode continued Storing Large Files Directories are files too Directories like files have inodes with attributes and pointers to disk blocks Directories are files too Directories like files have inodes with attributes and pointers to disk blocks Each directory contains the name and inode 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 inode for each file in the directory Directory functions include unistd h Change the directory int chdir const char path Directory functions include unistd h Change the directory int chdir const char path Get the current working directory char getcwd char buf size t size Directory functions include unistd h Change the directory int chdir const char path Get the current working directory char getcwd char buf size t size Directory reading functions include dirent h Open the directory DIR opendir const char dirname Directory reading functions include dirent h Open the directory DIR opendir const char dirname Close the directory int closedir DIR dirp Directory reading functions include dirent h Open the directory DIR opendir const char dirname Close the directory int closedir DIR dirp Read the directory struct dirent readdir DIR dirp What s in a directory entry struct dirent Member Fields char d name Null terminated file name …


View Full Document

U of I CS 241 - Lecture notes

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