Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
CS 423 – Operating Systems Design
Lecture 17 – Directory Concepts
and Implementation
Klara Nahrstedt
Fall 2011
Based on slides by YY Zhou and Andrew S. Tanenbaum
CS 423 - Fal 2011
CS 423 - Fal 2011
Overview
Administrative announcements
◦ MP2 – October 1
File organization
◦ Allocation: contiguous vs non-contiguous
Directory organization
◦ Single, two-level, tree-based organizations
◦ Links – hard and symbolic links, Paths
Summary
CS 423 - Fal 2011
File Allocation
Contiguous
Non-contiguous
◦ Linked Allocation
◦ Indexed Allocation
Tradeoffs?
Contiguous Allocation
CS 423 - Fal 2011
Contiguous Allocation Request in advance for the
size of the file
Search bit map or linked list to locate a space
File header
◦ first sector in file
◦ number of sectors
Pros
◦ Fast sequential access
◦ Easy random access
Cons
◦ External fragmentation
◦ Hard to grow files
CS 423 - Fal 2011
Linked Allocation
CS 423 - Fal 2011
CS 423 - Fal 2011
Linked Files (non-contiguous)
File header points to 1st block on disk
Each block points to next
Pros
◦ Can grow files dynamically
◦ Free list is similar to a file
Cons
◦ random access: horrible
◦ unreliable: losing a block means losing the rest
File header
null
. . .
Linked List Allocation using Table
File Allocation Table ◦ Put pointer word from each file
disk block into a table
◦ FAT in main memory
Pros ◦ Random access is much easier
◦ Chain of block pointers is in memory so it can be followed without making any disk reference
◦ Sufficient to keep only the starting block number – one can then access the full file independent how big the file is
Cons ◦ Problems?
CS 423 - Fal 2011
Indexed Allocation
CS 423 - Fal 2011
Indexed Allocation (i-node) Keep track of which disk
blocks belong to which file in i-node (index node)
In i-node ◦ addresses to fixed disk blocks (0-
7) (Why?)
◦ address of block pointer containing additional disk addresses (Why?)
Pros ◦ i-node in memory only when file is
open
◦ Easy random access
Cons ◦ What if each a file grows beyond
the limit of fixed number of disk addresses?
CS 423 - Fal 2011
CS 423 - Fal 2011
Questions
Which of the following is not a problem associated with
contiguous allocation of disk space for a file?
◦ External fragmentation of disk space or
◦ Frequent copying or
◦ Random access
Which of the following methods would you choose if
the file requires frequent direct access and also external
fragmentation is to be avoided (to keep disk utilization
high)?
◦ Linked allocation or
◦ Contiguous allocation or
◦ Indexed allocation
CS 423 - Fal 2011
Relation between PCBs, fd, open file table
and i-node table
CS 423 - Fal 2011
Directory Structure Organization
maps symbolic names into logical file
names
◦ search
◦ create file
◦ list directory
◦ backup, archival, file migration
CS 423 - Fal 2011
Directory Contents
file name symbolic name
file type indicates format of file
location device and location
size
protection
creation, access, and modification date
owner identification
CS 423 - Fal 2011
Single Level Directory
CS 423 - Fal 2011
Problems With Single Level
Directory more than one user
large file systems
moving files from one system to another
name clashes
modularity
CS 423 - Fal 2011
Two-level Directory
introduced to remove naming problems
between users
first level contains list of user directories
second level contains user files
system files kept in separate directory or
level 1
sharing accomplished by naming other
users files
CS 423 - Fal 2011
Two-level Directory
CS 423 - Fal 2011
Tree Structured Directories
arbitrary depth of directories
leaf nodes are files
interior nodes are directories
path name lists nodes to traverse to find
node
use absolute paths from root
use relative paths from current working
directory pointer
CS 423 - Fal 2011
Tree Structured Directories
CS 423 - Fal 2011
Acyclic Graph Structured Directories
Acyclic graphs allow sharing
two users can name same file
implementation by links - use logical
names of files (file system and file)
implementation by symbolic links maps
pathname into a new pathname
duplicate paths complicate backup copies
need reference counts for hard links
CS 423 - Fal 2011
Acyclic Graph Structured Directories
CS 423 - Fal 2011
Shared Files (1)
File system containing a shared file
CS 423 - Fal 2011
Shared Files (2)
(a) Situation prior to linking
(b) After the link is created
(c) After the original owner removes the file
CS 423 - Fal 2011
Hard Links and Symbolic Links
A file may be accessed through multiple paths ◦ Such linking between multiple names is known
as link (also known as hard link) UNIX allows users to make a new directory entry
that points to an existing file
4.3 BSD also supported symbolic links, which are files containing the path name of another file or directory ◦ Soft (symbolic) links, unlike hard links, could
point to directories and could cross file-system boundaries
CS 423 - Fal 2011
Hard Link Example
Grand Parent
directory
File inode 12345
This is the
Text in the
File. 23567
Block 23567
12345 Name 1
Directory entry in /dirA Directory entry in /dirB
12345 Name 2
CS 423 - Fal 2011
Symbolic Link Example
Grandparent
directory
File inode 12345
12345 Name 1
Directory entry in /dirA Directory entry in /dirB
13579 Name 2
This is the
Text in the
File. 23567
Block 23567
15213
“/dirA/name1”
Block 15213
Inode 13579
CS 423 - Fal 2011
Path Names
Absolute names:
•/usr/ast/mailbox
•Unique
•Start from root
Relative names:
•Working/current
directory
• mailbox
Examples:
•cp /usr/lib/dictionary .
•cp ../lib/dictionary .
CS 423 - Fal 2011
Looking up absolute path name
Lookup absolute path name /usr/ast/file
◦ Locate the root directory
◦ Lookup string „usr‟ in the root directory, get i-node of the /usr
directory
◦ Fetch i-node of /usr and extract disk blocks
◦ Search for the string „ast‟ in /usr, once the entry is found, the i-
node number for /usr/ast directory is taken from it
◦ Read i-node of /user/ast and extract directory blocks
◦ Lookup „file‟ and find i-node of „file‟
Conclusion: use of a relative path name is not only more
convenient, but also saves a substantial amount of work
for the system!!!
CS 423 - Fal 2011
Directory Operations
1. Create
• Direction creation
• Empty except . And ..
2. Delete
• Only empty directory can be deleted in some systems
• Directory with . And .. Is considered empty
3. Opendir
• This program opens the directory to list all files in the directory
• Before a directory can be read, it must be opened, analogous to opening and reading a file
4. Closedir
• When a directory has been read, it should be closed to free up internal table space
5. Readdir
• Returns the next entry in the open directory
6. Rename
• Rename directory just like any other file
7. Link
• Linking is a technique that allows a file to appear in more than one directory
• Creates a link between an existing file and the name specified by the path
• Link of this kind is called ‘hard link‟
• Each file which has links pointed to it, includes a reference counter in its i-node to keep track of the number of directory entries containing the file
8. Unlink
• If the file being unlinked is only present in one directory, it is removed from the file system
• If the file is present in multiple directories, only the path name specified is removed.
CS 423 - Fal 2011
Summary
Different directory organizations must be
considered for file systems due to
◦ size of the device
Single directory (embedded
devices/sensors)
One directory per user (handheld
devices)
Arbitrary tree per user
(PCs/laptops/workstations)