CS241 System ProgrammingFile System IVContentAdministrativeInheritance of File DescriptorsInheritance of File DescriptorsFilters and RedirectionRedirectionFile ControlDirectoriesUNIX File System NavigationPathDirectory AccessList files in a directoryFile StatusStruct StatDetermining the type of filePOSIX Macros for testing st_mode member for type of fileSummaryCS241 System ProgrammingFile System IVKlara NahrstedtLecture 233/15/20063/12/2006CS 241 - System Programming, Klara Nahrstedt2Content z Inheritance of file descriptorsz Filters and Redirectionz File Controlz File System Navigationz Directory Accessz Summary3/12/2006CS 241 - System Programming, Klara Nahrstedt3Administrative z R&R: Ch 4 pp92-135 z R&R: Ch 5 pp145-158z MP3 is posted, due April 3, 2006z Quiz 6 is March 17, 20063/12/2006CS 241 - System Programming, Klara Nahrstedt4Inheritance of File Descriptorsz “fork” creates a child and the child inherits a copy of most of the parent’s environment and context– Signal state– Scheduling parameters– File descriptor table[0][1][2][3][4]A(SFT)B(SFT)C(SFT)D(SFT)[0][1][2][3][4]A(SFT)B(SFT)C(SFT)D(SFT)ABCD(my.dat)System file table(SFT)ParentFile DescriptorTableChild’s File descriptorTableIf parent opens my.dat before forkingBoth parent and child share SFT entry3/12/2006CS 241 - System Programming, Klara Nahrstedt5Inheritance of File DescriptorsSystem file table(SFT)[0][1][2][3][4]A(SFT)B(SFT)C(SFT)D(SFT)ParentFile DescriptorTableABCD(my.dat)E(my.dat)[0][1][2][3][4]A(SFT)B(SFT)C(SFT)E(SFT)Child’s File DescriptorTableIf parent and child open my.dat after the forkCall, their file descriptor table entries point toDifferent SFT entries3/12/2006CS 241 - System Programming, Klara Nahrstedt6Filters and Redirectionz Filters – read standard input, perform transformation and output results to standard outputz Examples of useful UNIX filters– head, tail, more, sort, grep, awk, catz Redirection – action to redirect output– Program can modify the file descriptor table entry so that it points to a different entry in the SFTz Redirection uses– < redirection of standard input– > redirection of standard output3/12/2006CS 241 - System Programming, Klara Nahrstedt7Redirection#include <unistd.h>int dup2(int fildes, int fildes2); Copies file descriptor entry fildes to entry fildes2After closeAfter open After dup2Standard inputStandard outputStandard errorWrite to my.file[0][1][2][3]Standard inputWrite to my.fileStandard errorWrite to my.file[0][1][2][3]Standard inputWrite to my.fileStandard error[0][1][2]3/12/2006CS 241 - System Programming, Klara Nahrstedt8File Controlz fcntl – general purpose function for retrieving and modifying falgs associated with an open file descriptor#include <fcntl.h>#include <unistd.h>#include <sys/types.h>int fcntl(int fildes, int cmd, /*arg */. . . );Fildes – file descriptor, cmd – argument to specify operationz F_DUPFD – duplicate file descriptorz F_SETFD – set file descriptor flagsz F_GETFL – get file status flags and access modesDirectoriesR&R: Chapter 53/12/2006CS 241 - System Programming, Klara Nahrstedt10UNIX File System Navigationz Directory –– file containing directory entries– Associates a file name with physical location of a file on a diskz Tree-structured organization of a typical file system– / - root directory– Absolute – fully qualified pathnamesz /dirA/dirB/my1.dat– Current working directoryz ./../my2.dat (/dirA/my2.dat)z ../dirB/my1.dat (/dirA/dirB/my1.dat)3/12/2006CS 241 - System Programming, Klara Nahrstedt11Path•PWD environment variable – specifies current working directory•Chdir function – causes directory specified by path to become current working directory#include <unistd.h>int chdir(const char *path);•Getcwd function – returns pathname of the current directorychar * getcwd(char *buf, size_t size);•Other path functions•fpathconf /pathconf – report limits associated with a particular file or directory3/12/2006CS 241 - System Programming, Klara Nahrstedt12Directory Accessz Directories should not be accessed with the ordinary open, close and read functions. z We need specialized functions– Opendir– Closedir– Readdir#include <dirent.h>DIR *opendir(const char *dirname);struct dirent *readdir(DIR *dirp); int closedir(DIR *dirp);void rewinddir(DIR *dirp);3/12/2006CS 241 - System Programming, Klara Nahrstedt13List files in a directory#include <dirent.h>#include <stdio.h>int main(int argc, char *argv[]) {struct dirent *direntp;DIR *dirp;if (argc != 2) {fprintf(stderr, "Usage: %s directory_name\n", argv[0]);return 1; } if ((dirp = opendir(argv[1])) == NULL) {perror ("Failed to open directory");return 1;} while ((direntp = readdir(dirp)) != NULL)printf("%s\n", direntp->d_name);while ((closedir(dirp) == -1) && (errno == EINTR)) ;return 0;}3/12/2006CS 241 - System Programming, Klara Nahrstedt14File Status#include <sys/stat.h>int stat(const char *restrict path, struct stat *restrict buf);int lstat(const char *restrict path, struct stat *restrict buf);•Lstat returns status info and info about symbolic links•Stat returns status info of files referred to by any links3/12/2006CS 241 - System Programming, Klara Nahrstedt15Struct Statdev_t st_dev; /* device ID of device containing file*/ino_t st_ino; /* file inode number */mode_t st_mode; /* file mode*/nlink_t st_nlink; /* number of hard links */uid_t st_uid; /*user id of file*/gid_t st_guid; /* group id of fileoff_t st_size; /* file size in bytes*/time_t st_atime; /* time of last access*/time_t st_mtime;/* time of last data modification*/time_t st_ctime; /* time of last file status change*/3/12/2006CS 241 - System Programming, Klara Nahrstedt16Determining the type of filez st_mode – file mode member – Specifies the access permissions of the file and the type of filez Types of file – Regular file – randomly accessible sequence of bytes with no further structure imposed by the system – Special files – specify devicesz Character special files – represent devices such as terminalsz Block special files – represent disk devices3/12/2006CS 241 - System Programming, Klara Nahrstedt17POSIX Macros for testing st_modemember for type of filemacro tests forS_ISBLK(m) block special fileS_ISCHR(m) character special fileS_ISDIR(m) directoryS_ISFIFO(m) pipe or FIFO special fileS_ISLNK(m) symbolic linkS_ISREG(m) regular fileS_ISSOCK(m) socket3/12/2006CS 241 - System Programming, Klara Nahrstedt18Summaryz UNIX Indirection, Filters
View Full Document