File Management
File Management
Requirements
Requirements
• For long-term storage:
ibl l f i f
– possible to store large amount of info. – info must survive termination of
processes processes
– multiple processes must be able to access concurrently
• Solution: store in units called files • Filesystem: part of the OS
responsible for providing file services to users & apps
File Management Functions
File Management Functions
• Identify and locate a selected file • Use a directory to describe the
location of all files plus their tt ib t
attributes
• Describe shared user access t l
control
• Blocking for access to files • Allocate files to free blocks
• Manage free storage for available blocks
Files
• You should already be familiar with:
– types (regular, directory, character/block special)
– naming – naming
– protections (e.g., -rwxr-xr-x bits in Linux) – operations (create, delete, open, read, etc.)p ( , , p , , )
• Two types of file access:
– Sequential: read all bytes/records from q y / beginning
– Random: read in any order
k t t t i l ti i fil • use seek to move to certain location in file • essential for database files
File Organization
File Organization
• How are files organized physically? • Five main organizations:
– pile
– sequential file
– indexed sequential fileq – indexed file
File Organization
File Organization
• The Pile
– Data collected in chronological order
– Records may have different fields – No structure
– Record access is by exhaustive search
Pile
Pile
Sequential File
Sequential File
• Fixed length records
• All fields the same (order and length)
• Key field
– Uniquely identifies the record
– Records are stored in key sequence
• New records go in separate log fileg p g
Sequential File
Sequential File
Indexed Sequential File
Indexed Sequential File
• Index provides a fast lookup capability
capability
• Contains key field and a pointer to i fil
main file
• Only select indices from main file are stored in the index (requires some sequential search of file)
• New records stored in separate overflow file
Indexed Sequential File
Indexed Sequential File
Indexed File
Indexed File
• Multiple indices for different key • Multiple indices for different key
fields
• Exhaustive index: one entry per • Exhaustive index: one entry per
record
P ti l i d l t d f • Partial index: only to records of
interest
• No restriction on placement of records
Indexed File
Indexed File
Direct (hashed) file
Direct (hashed) file
• Directly access a block at a Directly access a block at a known address
• Key field required for each • Key field required for each
record
• For very fast access • For very fast access
Directories
Directories
• What is a directory?
• Directory: a file owned by the OS
– accessible by file management routinesg – contains info about the files
• attributes, location, ownership
• You should already be familiar with:
– pathnames
– naming directories – permissions
File Directories
File Directories
File Directories
File System
I
l
t ti
Implementation
A possible file system layout A possible file system layout
Physical Allocation of
Fil
Files
• How are files stored on disk? • Several approaches:
– contiguous allocationg – chained allocation
– indexed allocation
• Need some data structure to keep track of where files are on disk: file allocation table (FAT)
• Also need to track which disk blocks f
Methods of File
All
ti
Allocation
• Contiguous allocationContiguous allocation
– Contiguous set of blocks is allocated to a file at the time of creation
– Only a single entry in the file allocation table
• Starting block and length of the file
Contiguous Allocation
Contiguous Allocation
Methods of File
All
ti
Allocation
• Chained allocation
– Allocation on basis of individual block
– Each block contains a pointer to the next block
block
– Only single entry in the file allocation table
• Starting block and length of file
• No external fragmentation Best for sequential files
• Best for sequential files
• No accommodation of the principle of locality
Chained Allocation
Methods of File
All
ti
Allocation
• Indexed allocationIndexed allocation
– File allocation table contains a separate one-level index for each file
– The index is a block
– Index has one entry for each portion Index has one entry for each portion allocated
Indexed Allocation:
V i bl L
th P ti
FATs vs. I-nodes
FATs vs. I nodes
• File Allocation Table (FAT):
– tracks location of files on disk
– disadvantage: entire table must be in RAM
• Index-nodes (I-nodes):( )
– small data structure, one per file
– lists attributes and disk addresses of s s a bu s a d d s add ss s o file’s blocks
– advantage: I-node need be in RAM g only when file is opened
I-nodes on Disk
I nodes on Disk
Implementing Directories
(a) A simple directory (a) A simple directory
fixed size entries
disk addresses and attributes in directory entry (b) Directory in which each entry just refers to (b) Directory in which each entry just refers to
Implementing Directories
• Two ways of handling long file names in di ecto
directory
– (a) In-line
Free Space Management
Free Space Management
• Must know what blocks on disk are available • Bit Tables
– One bit per block on disk
– Still can be sizeable -- consider searching!
• Chained Free Portions • Chained Free Portions
– Chain together free blocks on disk – Disk will become fragmented
Indexing • Indexing
– Use an index table like file allocation
• Free Block List
– Each free block is numbered and stored in loc on disk – Doesn’t require search like bit table