DOC PREVIEW
U of I CS 241 - File System Implementation

This preview shows page 1-2-22-23 out of 23 pages.

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

Unformatted text preview:

File System ImplementationContentsUNIX file structure implementationSystem File TableOpening and Closing FilesFle Access ExampleAdditional OflagsAccess Control sys/stat.hCloseSELECTFile Pointers and BufferingFilters and RedirectionDirectoriespathList files in a directoryFile StatusStruct StatPOSIX Macros for mode_tUNIX Special FilesPipesParent writes string to ChildSlide 22Summary01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved1File System ImplementationCS 241 Lecture 23R: Ch 4-6 pp116-186Roy Campbell01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved2ContentsFile System TablesOpen CloseFile Permissions/Access ControlDirectoriesSpecial FilesPipes01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved3UNIX file structure implementationFile positionR/WPointer to inodeFile positionR/WPointer to inodeModeLink CountUIDGIDFile sizeTimesAddress of first 10 disk blocksSingle IndirectDouble IndirectTriple IndirectInode in memoryOpen file descriptionParent File descriptortableChildFile descriptortableUnrelated processFile descriptor tableSystem file table01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved4System File TableOne per system – is in kernelContains file offset, access mode, count of file descriptor entries using itSeveral entries may correspond to one fileThe in-memory inode table has one entry for each active file.01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved5Opening and Closing FilesOpen associates a file descriptor with a file or physical deviceFile descriptor is an index into a table per process and can be inherited to child processes allowing sharingShould close open filesOflag access modes and status01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved6Fle Access Exampleint fd;mode_t fdmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);If ((fd = open(“info.dat”, O_RDWR | O_CREAT, fdmode)) == -1) perror(“Failed to open info.dat”);Opens a file info.dat in current directory rewriting any existing file dataif present.01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved7Additional OflagsO_APPEND – file offset to EOFO_CREAT – need to give permissionsO_EXCL – use with CREATE detects fileO_NOCTTY – prevents device from becoming controlling terminalO_NONBLOCK – return immediatelyO_TRUNC – file to beginning for write01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved8Access Control sys/stat.hR W X R W X R W Xusergroup othersS_IRUSRS_IWUSRS_IXUSRS_IRWXUS_IRGRPS_IWGRPS_IXGRPS_IRWXGS_IROTHS_IWOTHS_IXOTHS_IRWXOS_ISUID – set user ID on executionS_ISGID – set group ID on execution01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved9Close#include <errno.h>#include <unistd.h>int r_close(int fd) { int retval; while ((retval = close(fd)) == -1 && errno == EINTR) ; return retval;}01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved10SELECTMonitoring multiple files – use a process per file or use theSelect statement – can select input from one of a number of different files01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved11File Pointers and BufferingISO C standard I/O Library uses file pointers not file descriptors (p122)fflush call empties buffer to deviceFiles associated with terminals are line buffered – ie not written out until buffer is full or newline symbol entered01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved12Filters and RedirectionFilters in shellhead, tail, more, sort, grep, awk, catRedirection uses < >#include <unistd.h>int dup2(int fildes, int fildes2); Copies file descriptor entry fildes to entry fildes201/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved13DirectoriesCh 501/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved14path#include <unistd.h>int chdir(const char *path);char * getcwd(char *buf, size_t size);01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved15List 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;01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved16File 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 links01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved17Struct Stat dev_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 file off_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*/01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved18POSIX Macros for mode_tmacro 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) socket01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved19UNIX Special FilesCh 601/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved20Pipes#include <unistd.h>int pipe(int fildes[2]);Creates 2 file descriptorse.g.Int fd[2];If (pipe(fd) == -1) perror(“Failed to create the pipe”);01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved21Parent writes string to Child#include <stdio.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#define BUFSIZE 10 int main(void) { char bufin[BUFSIZE] = "empty"; char bufout[] = "hello"; int bytesin; pid_t childpid; int fd[2]; if (pipe(fd) == -1) { perror("Failed to create the pipe"); return 1; }01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved22Parent writes string to Childbytesin = strlen(bufin); childpid = fork(); if (childpid == -1) { perror("Failed to fork"); return 1; } if (childpid) /* parent code */ write(fd[1], bufout, strlen(bufout)+1); else


View Full Document

U of I CS 241 - File System Implementation

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 File System Implementation
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 File System Implementation 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 File System Implementation 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?