Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 31: Introduction to File System

Similar presentations


Presentation on theme: "Lecture 31: Introduction to File System"— Presentation transcript:

1 Lecture 31: Introduction to File System

2 Overview of this course
OS is a resource manager Process/threads Deadlocks Memory Disk, i.e., file system

3 File system Files and directories Implementing a file system on disk
Management and optimization

4 Permanent Storage USB Disk SSD Disk Disk
File systems provide a simple abstraction of permanent storage, i.e., storage that doesn’t go away when your program terminates or you shut down the computer. A user accesses information, represented as files, and the operating system is responsible for retrieving and storing this information. We expect files to last a long time and we associate names (as opposed to addresses) with files. SSD

5 Files USB Disk Disk Disk File systems provide a simple abstraction of permanent storage, i.e., storage that doesn’t go away when your program terminates or you shut down the computer. A user accesses information, represented as files, and the operating system is responsible for retrieving and storing this information. We expect files to last a long time and we associate names (as opposed to addresses) with files. File system provides a simple abstraction of permanent storage SSD

6 File System USB Disk User access
A user accesses information, represented as files and directories. The OS is responsible for retrieving and storing this information.

7 Roles of OS Two important tasks of an operating system
1. Provides easy interface to hardware 2. Manages resources

8 Requirements Permanent storage (usually) User access
resides on disk (or alternatives) survives software and hardware crashes User access Regular users Programmers (convenient programming interface) File systems are responsible for permanent storage. This usually (but not always) means that disks are used as the storage medium.

9 Examples of file systems
MS-DOS Windows ext2 file system Linux HFS/HFS+ OS X CD-ROM file systems Often files known in advance

10 Files Naming Structure Types Access Attributes Operations

11 File naming File name + extension File name Extension: e.g., myprog.c
ASCII characters Unicode Case sensitive, e.g., Unix Case insensitive, Windows Extension: Determine which programs to use to open the file E.g., clicking on “myfile.doc” opens up Microsoft word

12 Typical file extensions.

13 File structure Byte sequence Record sequence Tree of records Popular
Not that popular now Tree of records Large mainframe computers

14 Types of Unix Files Block special files Regular files Directories
ASCII files – lines of text Special programs to handle these (wc, grep) Binary files executable code, archives, .dvi , .pdf Special structures Directories special files whose data contains a list of files Character Special Files Point to I/O devices, e.g., printers, terminals, networks Block special files Model disk

15 Special Files ls –l /dev
crw root root , 3 Apr atimouse crw root root , 4 Apr audio crw root root , 20 Apr audio1 crw root root , 7 Apr audioctl brw-rw root disk , 0 Apr aztcd Character Special Files (terminal, network device, etc) The device is abstracted by a stream of bytes that can only be accessed in sequential order. Block Special Files (disks) The device driver transfers chunks or blocks of data between the operating system and the device.

16 File access Sequential access Random access Start at the beginning
Cannot skip around E.g., magnetic tape Random access Directly read data at a position

17 File Attributes Information about the file, but not a part of the file data Protection (or permissions) Owner Time of creation Current Size Etc.. Examples (in Unix) -rw-r--r snt users Sep 25 13:49 switching-networks.ps drwxr-xr-x 4 snt users Oct 7 20:07 streams

18 File Attributes Information about the file, but not a part of the file data Protection (or permissions) Owner Time of creation Current Size Etc.. Examples (in Unix) -rw-r--r snt users Sep 25 13:49 switching-networks.ps drwxr-xr-x 4 snt users Oct 7 20:07 streams

19 File operations int open(const char *pathname, int flags, mode_t mode); Returns a file descriptor ssize_t read(int fildes, void *buf, size_t count); Use the file descriptor ssize_t write(int fildes, const void *buf, size_t count); Use file descriptor off_t lseek(int fildes, off_t offset, int whence); Delete, read attributes, set attributes, etc

20 the open() call int open(const char *path, int flags,[mode_t mode]);
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *path, int flags,[mode_t mode]); char *path: is a string that contains the fully qualified filename of the file to be opened. int flags: specifies the method of access i.e. read_only (O_RDONLY), write_only (O_WRONLY), read_and_write (O_RDWR). mode_t mode: optional parameter used to set the access mode upon file creation (e.g. O_TRUNC, O_APPEND, …)

21 read() and write() #include <fcntl.h>
ssize_t read(int filedes, void *buffer, size_t n); ssize_t write(int filedes, const void *buffer, size_t n); int filedes: file descriptor that has been obtained through an open() or create() call. void *buffer: pointer to an array that will hold the data that is read or holds the data to be written. size_t n: the number of bytes that are to be read/written from/to the file.

22 An example #include <fcntl.h> #include <unistd.h> main()
{ int fd; /* a file descriptor */ ssize_t nread; /* # of bytes read */ char buf[1024]; /* data buffer */ /* open the file “data” for reading */ fd = open(“C:/mydata.dat”, O_RDONLY); /* read in the data */ nread = read(fd, buf, 1024); /* close the file */ close(fd); }

23 A close() call #include <fcntl.h> int close(int filedes);
Although all open files are closed by the OS upon completion of the program, it is good programming style to “clean up” after you are done with any system resource. Please make it a habit to close all files that your program has used as soon as you don’t need them anymore! #include <fcntl.h> int close(int filedes); Benefits:?

24 A close() call #include <fcntl.h> int close(int filedes);
Although all open files are closed by the OS upon completion of the program, it is good programming style to “clean up” after you are done with any system resource. Please make it a habit to close all files that you program has used as soon as you don’t need them anymore! #include <fcntl.h> int close(int filedes); Remember, closing resources timely can improve system performance and prevent deadlocks from happening (more later)

25 Random Access #include <sys/types.h> #include <unistd.h>
off_t lseek(int fd, off_t offset, int whence) sets the file pointer for fd: if whence is SEEK_SET, the pointer is set to offset bytes; if whence is SEEK_CUR, the pointer is set to its current value plus offset bytes; if whence is SEEK_END, the pointer is set to the size of the file plus offset bytes it returns the (possibly) updated value of the file pointer relative to the beginning of the file. Thus, n = lseek(fd, (off_t)0, SEEK_CUR); returns the current value of the file pointer for fd To effect random access to files (i.e., access files other than sequentially), you first set the file pointer, then perform a read or write. Setting the file pointer is done with the lseek system call.

26 Standard File Descriptors
main( ) { char buf[100]; int n; const char* note = "Write failed\n"; while ((n = read(0, buf, sizeof(buf))) > 0) if (write(1, buf, n) != n) { (void)write(2, note, strlen(note)); exit(EXIT_FAILURE); } return(EXIT_SUCCESS); File descriptor 0: standard input File descriptor 1: standard output File descriptor 2: standard error output The file descriptors 0, 1, and 2 are opened to access your terminal when you log in, and are preserved across forks, unless redirected.

27 Directories Directory systems Path names Operations

28 Directory systems Single-level directory systems
Root directory includes all files Hierarchical directory systems

29 Hierarchical directory systems
doc etc home pro dev a letter mod snt b c prs ... Here is a portion of a Unix directory tree. The ovals represent files, the rectangles represent directories (which are really just special cases of files). slide1 slide2 : A directory : A file d e

30 Path names Absolute path name Relative path name
E.g., /usr/ast/mailbox Relative path name Relative to the working directory (also called current directory) E.g., mailbox if the working directory is /usr/ast

31 Directory operations Create Delete Opendir Closedir Readdir Rename
Link Unlink


Download ppt "Lecture 31: Introduction to File System"

Similar presentations


Ads by Google