Operating Systems Unit - 5: I/O and File Management
Operating Systems Unit - 5: I/O and File Management
Operating Systems Unit - 5: I/O and File Management
Outline
File Concept and Structure Directory Structures File Organizations Access Methods Protection
File Concept
OS abstracts from the physical properties of its storage device to define a logical storage unit called file. OS maps files to physical devices.
Data
Types
Program
Documents
File Structure
Lines Fixed Length Variable Length Formatted document Relocatable Load File
Complex Structures
Can simulate last two with first method by inserting appropriate control characters Who decides
File Attributes
Name
symbolic file-name, only information in human-readable form for systems that support multiple types pointer to a device and to file location on device
Type
Location Size
Information about files are kept in the directory structure, maintained on disk
File Operations
Create a file Write a file Read a file Reposition within file - file seek Delete a file Truncate a file Open(Fi)
search the directory structure on disk for entry Fi, and move the content of entry to memory. move the content of entry Fi in memory to directory structure on disk.
Close(Fi)
Possible extension
Exe,com,bin Obj, o c, CC, p, java, asm Bat, sh Txt, doc ps, dvi, gif Arc, zip, tar Lib, a
Function
Machine language program Compiled machine lang., not linked Source code in various languages Commands to command interpreter Textual data, documents ASCII or binary file Group of files, sometimes compressed Libraries of routines
Directory Structure
Break file systems into partitions ( treated as a separate storage device) Hold information about files within partitions.
Device Directory: A collection of nodes containing information about all files on a partition. Both the directory structure and files reside on disk. Backups of these two structures are kept on tapes.
File Name File Type Address or Location Current Length Maximum Length Date created, Date last accessed (for archival), Date last updated (for dump) Owner ID (who pays), Protection information
Search for a file Create a file Delete a file List a directory Rename a file Traverse the filesystem
Two users can have the same name for different files. The same file can have several different names. Logical grouping of files by properties (e.g. all Pascal programs, all games)
Grouping
A single directory for all users Naming Problem and Grouping Problem
As the number of files increases, difficult to remember unique names As the number of users increase, users must have unique names.
First Level contains list of user directories Second Level contains user files Need to specify Path name Can have same file names for different users. System files kept in separate directory or Level 1. Efficient searching
Absolute from root Relative paths from current working directory pointer.
Creating a new file is done in current directory Creating a new subdirectory is done in current directory, e.g. mkdir <dir-name> Delete a file , e.g. rm file-name Deletion of directory
Option 1 : Only delete if directory is empty Option 2: delete all files and subdirectories under directory
Links are pointers to other files or subdirectories Symbolic links or relative path name
Directory entry is marked as a link and name of real file/directory is given. Need to resolve link to locate file.
Duplicate information in sharing directories Original and copy indistinguishable. Need to maintain consistency if one of them is modified.
Traversal
ensure that shared data structures are traversed only once.
Deletion
Removing file when someone deletes it may leave dangling pointers. Preserve file until all references to it are deleted
Keep a list of all references to a file or Keep a count of the number of references - reference count. When count = 0, file can be deleted.
Need to ensure that components are not traversed twice both for correctness and for performance, e.g. search can be non-terminating. Need garbage collection mechanism to determine if file can be deleted.
Access Methods
Sequential Access
read next write next reset no read after last write (rewrite)
Protection
Types of access
Problem - length of access list.. Mode of access: read, write, execute Three classes of users
owner access - user who created the file groups access - set of users who are sharing the file and need similar access public access - all other users Fields are user, group, others(u,g,o), Bits are read, write, execute (r,w,x). E.g. chmod go+rw file , chmod 761 game
File-System Implementation
File System Structure Allocation Methods Free-Space Management Directory Implementation Efficiency and Performance Recovery
File-System Structure
File Structure
Logical Storage Unit with collection of related information To improve I/O efficiency, I/O transfers between memory and disk are performed in blocks.
File system organized into layers. File control block - storage structure consisting of information about a file.
File System must be mounted before it can be available to process on the system
The OS is given the name of the device and the mount point (location within file structure at which files attach). OS verifies that the device contains a valid file system. OS notes in its directory structure that a file system is mounted at the specified mount point.
Low level access methods depend upon the disk allocation scheme used to store file data
Contiguous Allocation
Simple - only starting location (block #) and length (number of blocks) are required. Suits sequential or direct access. Fast (very little head movement) and easy to recover in the event of system crash.
Problems
Wasteful of space (dynamic storage-allocation problem). Use first fit or best fit. Leads to external fragmentation on disk. Files cannot grow - expanding file requires copying Users tend to overestimate space - internal fragmentation.
Block to be accessed = Q + starting address Displacement into block = R
Contiguous Allocation
Linked Allocation
Blocks may be scattered anywhere on the disk. Each node in list can be a fixed size physical block or a contiguous collection of blocks. Allocate as needed and then link together via pointers.
Disk space used to store pointers, if disk block is 512 bytes, and pointer (disk address) requires 4 bytes, user sees 508 bytes of data.
pointer Block = Data
Linked Allocation
Linked Allocation
Simple - need only starting address. Free-space management system - space efficient.
Suited for sequential access but not random access. Directory Table maps files into head of list for a file. Mapping - <Q, R>
Block to be accessed is the Qth block in the linked chain of blocks representing the file. Displacement into block = R + 1
Need to read through linked list nodes sequentially to find the record of interest.
System crashes can scramble files being updated.
Indexed Allocation
Brings all pointers together into the index block. Logical view
Index table
Indexed Allocation
40
Need index table. Supports sequential, direct and indexed access. Dynamic access without external fragmentation, but have overhead of index block.
Mapping from logical to physical in a file of maximum size of 256K words and block size of 512 words. We need only 1 block for index table.
Mapping - <Q,R>
Link blocks of index tables (no limit on size) E.g. Two Level Index - first level index block points to a set of second level index blocks, which in turn point to file blocks. Increase number of levels based on maximum file size desired. Maximum size of file is bounded.
Multilevel Index
link
link
Index block
link link
Direct blocks data data Single indirect double indirect Triple indirect data data data data data
45
Eg. Block size = 2^12 bytes, Disk size = 2^30 bytes n = 2^30/2^12 = 2^18 bits ( or 32K bytes)
Keep a linked list of free blocks Cannot get contiguous space easily, not very efficient because linked list needs traversal. No waste of space
Keep a linked list of index blocks. Each index block contains addresses of free blocks and a pointer to the next index block. Can find a large number of free blocks contiguously.
Counting
Linked list of contiguous blocks that are free Free list node contains pointer and number of free blocks starting from that address.
Need to protect
Must be kept on disk Copy in memory and disk may differ. Cannot allow for block[i] to have a situation where bit[i] = 1 in memory and bit[i] = 0 on disk Set bit[i] = 1 in disk Allocate block[i] Set bit[i] = 1 in memory.
Solution
Directory Implementation
simple to program time-consuming to execute - linear search to find entry. Sorted list helps - allows binary search and decreases search time. decreases directory search time collisions - situations where two file names hash to the same location. Each hash entry can be a linked list - resolve collisions by adding new entry to linked list.
disk allocation and directory algorithms types of data kept in the files directory entry Dynamic allocation of kernel structures
On-board cache - for disk controllers Disk Cache - separate section of main memory for frequently used blocks. Block replacement mechanisms
LRU Free-behind - removes block from buffer as soon as next block is requested. Read-ahead - request block and several subsequent blocks are read and cached.
Recovery
Ensure that system failure does not result in loss of data or data inconsistency. Consistency checker
compares data in directory structure with data blocks on disk and tries to fix inconsistencies. Use system programs to back up data from disk to another storage device (floppy disk, magnetic tape). Recover lost file or disk by restoring data from backup.
Backup
Restore