Unformatted text preview:

CS 241 Section Week #8(4/2/09)Outline• LMP1 Overview• Files & I/O• UNIX File Systems– inodes– Directories– LinksLMP1 OverviewLMP1 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);open()• int open(const char *pathname,int flags);• open() returns an int, which is your file descriptoropen()• int open(const char *pathname,int flags);• open(…) takes in either a relative or full path name– ../../../../../usr/importantfile.txtopen()• 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), fopenreturns 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)– 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 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);Directory functions#include <unistd.h>Change the directoryint chdir(const char *path);Get the current working directorychar *getcwd(char *buf, size_t size);Directory functions#include <unistd.h>Change the directoryint chdir(const char *path);Get the current working directorychar *getcwd(char *buf, size_t size);Directory reading functions#include <dirent.h>Open the directoryDIR


View Full Document

ILLINOIS CS 241 - Section Week #8

Download Section Week #8
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 Section Week #8 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 Section Week #8 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?