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