300 likes | 598 Views
CS 217 - Operating Systems. File-System Interface Ayesha Afzal NFC IET. File-System Interface. File Concept Access Methods Directory Structure. Objectives. To explain the function of file systems To describe the interfaces to file systems
E N D
CS 217 - Operating Systems File-System Interface Ayesha Afzal NFC IET
File-System Interface • File Concept • Access Methods • Directory Structure
Objectives • To explain the function of file systems • To describe the interfaces to file systems • To discuss file-system design tradeoffs, including access methods, file locking, and directory structures
File Concept • OS’s abstraction of physical properties of the storage devices to define a logical storage unit, the “File” • File is a named collection of related information that is recorded on secondary storage • Contiguous logical address space • User’s Perspective: Smallest allotment of secondary storage • Types: • Data • numeric • character • binary • Program
File Structure • Sequence of words, bytes, bits; meanings defined by creator/user • Simple record structure • Lines • Fixed length • Variable length • Complex Structures • Formatted document • Relocatable load file • Examples: • Text Files: sequence of characters organized into lines/pages • Source Files: sequence of subroutines & functions each of which is further organized as declarations followed by executable instructions
File Attributes • Name – only information kept in human-readable form • Identifier – unique tag (number) identifies file within file system • Type – needed for systems that support different types • Location – pointer to file location on device • Size – current file size • Protection – controls who can do reading, writing, executing • Time, date, and user identification – data for protection, security, and usage monitoring • Information about files are kept in the directory structure, which is maintained on the disk
File Operations OS provides system calls for these operations • File is an abstract data type • Create • Find space + directory entry • Write • System call with file name and data to write • Directory traversal to locate file • Positioning of Write pointer and updating it upon write • Read • System call specifying the file name and read location • Directory traversal to locate file • Positioning of Read pointer and updating it upon read • Reposition within file • Repositioning the current-file-position pointer • File seek: no actual I/O involved
File Operations (contd.) OS provides system calls for these operations • Delete • Search directory for named file • Release all file space • Erase directory entry • Truncate • If user wants to delete file contents but keep the attributes • All attributes remain unchanged but file length • Lets file to be reset to length zero and its space released • Open(Fi) – search the directory structure on disk for entry Fi, and move the content of entry to memory • Close (Fi) – move the content of entry Fi in memory to directory structure on disk
File Operations (contd.) • Open(Fi)– search the directory structure on disk for entry Fi, and move the content of entry to memory • Takes file name • Searches directory • Copies directory entry into Open-File table (contains info of all open files) • Can also accept access-mode information (create, read-only, read-write, append-only etc.) • Typically returns a pointer to an entry in open-file table which is used in all further I/O operations, avoiding any further searching • Close (Fi)– move the content of entry Fi in memory to directory structure on disk • Open-File table may include an open count for all open processes, close operation decrements this count. When this count for a file is reduced to zero, no process is using the file so the file entry is removed from open-file table
OS uses two levels of internal tables: • Per-process table • Tracks all files a process has opened • Each entry may include information regarding, current file pointer, access rights and accounting information • System-wide table • Each entry in per-process table points to a system-wide table • Contains process-independent information e.g., location of file on disk, access dates and file size etc. • Once a file has been opened for a process P1 via Open(Fi), • system-wide table includes an entry for the file, Fi • Open(Fi) system call from process P2: an new entry is added to the P2’s open-file table pointing to appropriate entry in system-wide table
Open Files • Several pieces of data are needed to manage open files: • File pointer: • pointer to last read/write location, per process that has the file open • File-open count: • counter of number of times a file is open – to allow removal of data from open-file table when last processes closes it • Disk location of the file: • cache of data access information(information need to locate the file on disk) • Access rights: • per-process access mode information
Open File Locking • Provided by some operating systems and file systems • Mediates access to a file • Shared lock (akin to reader lock) • Exclusive lock (akin to writer lock) • Mandatory or advisory: • Mandatory – access is denied depending on locks held and requested • E.g., If one process has acquired exclusive lock, all others are denied access till the exclusive-lock is released • Advisory – processes can find status of locks and decide what to do • Windows OS implements Mandatory locking • UNIX OS implements Advisory locking
Today’s Objectives • Access Methods • Directory Structure
Access Methods • Sequential Access • Information processed in order, one record after the other • E.g., editors, compilers read next write next reset no read after last write (rewrite) • Direct Access • Process file by accessing the content in no specific order. • For example, if we only want to read page 1013 it makes sense to reposition (seek) to page 1013 and read the page. read n write n position to n read next write next rewrite n n = relative block number
Other Access Modes • Random • Read or write the nth record or bytes i through j. • Unix provides an equivalent facility by adding a seek operation to the sequential operations listed earlier. This packaging of operations allows random access but encourages sequential access. • Indexed • Read or write the record with a given key. • In some cases, the “key” need not be unique--there can be more than one record with the same key. In this case, programs use a combination of indexed and sequential operations: • Get the first record with a given key, then get other records with the same key by doing sequential reads.
Directory Structure • A collection of nodes containing information about all files Directory Files F 1 F 2 F 3 F 4 F n Both the directory structure and the files reside on disk Backups of these two structures are kept on tapes
Disk Structure Disk can be subdivided into partitions Disks or partitions can be RAID protected against failure Disk or partition can be used raw– without a file system, or formattedwith a file system Partitions also known as minidisks, slices Entity containing file system known as a volume Each volume containing file system also tracks that file system’s info in device directoryor volume table of contents As well as general-purpose file systemsthere are many special-purpose file systems, frequently all within the same operating system or computer
Operations Performed on Directory • Search for a file • Create a file • Delete a file • List a directory • Rename a file • Traverse the file system
Organize the Directory (Logically) to Obtain: • Efficiency – locating a file quickly • Naming – convenient to users • Two users can have same name for different files • The same file can have several different names • Grouping – logical grouping of files by properties, (e.g., all Java programs, all games, …)
Single-Level Directory • A single directory for all users • Naming problem • Grouping problem
Two-Level Directory • Separate directory for each user • UFDs – User File Directories • Can have the same file name for different user • Efficient searching • Protection of users files • Users are completely independent! No grouping and difficult to cooperate on tasks by multiple users • Path names – for userA to access userB’s file, one needs to refer to /userB/filename
Tree-Structured Directories (Cont.) • Most common directory structure • Generalization allows users to create their own sub-directories and organize their files • Efficient searching • Grouping Capability • One Root directory • Every file in the system has a unique Path Name • A directory/subdirectory contains a set of files and sub-directories • Directory is simply another file, treated in a special way – contains directory entries for all its contents • A bit in every directory entry defines the entry as a file (0) or as a subdirectory (1) • Every process has a current directory, containing most of the files of current interest to the process • If a searched file is not in current directory, user must provide complete path of the file or change current directory to the holding directory of the file
Tree-Structured Directories (Cont) • Absolute or relative path name • Absolute Path: begins at the root and and follows a path down to specified file • Relative Path: defines path from current directory • Creating a new file is done in current directory • Delete a file rm <file-name> • Creating a new subdirectory is done in current directory mkdir <dir-name> Example: if in current directory /mail mkdir count mail prog copy prt exp count Deleting “mail” deleting the entire sub-tree rooted by “mail”
Acyclic-Graph Directories • This structure allows directories to share sub-directories and files. • The sharing is achieved by creating links that point to another file or directory, implemented as an absolute or relative path