Week 11
Week 11
References:
1.Silberschatz, Abraham, Peter B. Galvin, and Greg Gagne. Operating
system concepts. Wiley Publishing, 2013.
2.Stallings, William. Operating Systems 5th Edition. Pearson Education
India, 2006.
3.Tannenbaum, Andrew S. "Modern Operating Systems, 2009."
File Concept
• Contiguous logical address space
• Types:
• Data
• numeric
• character
• binary
• Program
• Contents defined by file’s creator
• Many types
• Consider text file, source file, executable file
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
• Many variations, including extended file attributes such as file
checksum
• Information kept in the directory structure
File info Window on Mac OS X
File Operations
• File is an abstract data type
• Create
• Write – at write pointer location
• Read – at read pointer location
• Reposition within file - seek
• Delete
• Truncate
• 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
Open Files
• Several pieces of data are needed to manage
open files:
• Open-file table: tracks 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
• Access rights: per-process access mode information
Open File Locking
• Provided by some operating systems and file
systems
• Similar to reader-writer locks
• Shared lock similar to reader lock – several
processes can acquire concurrently
• Exclusive lock similar to writer lock
• Mediates access to a file
• Mandatory or advisory:
• Mandatory – access is denied depending on locks
held and requested
• Advisory – processes can find status of locks and
decide what to do
File Types – Name, Extension
File Structure
• None - sequence of words, bytes
• Simple record structure
• Lines
• Fixed length
• Variable length
• Complex Structures
• Formatted document
• Relocatable load file
• Can simulate last two with first method by inserting
appropriate control characters
• Who decides:
• Operating system
• Program
Sequential-access File
Access• Methods
Sequential Access
read next
write next
reset
no read after last write
(rewrite)
• Direct Access – file is fixed length logical records
read n
write n
position to n
read next
write next
rewrite n
n = relative block number
Directory
Files
F1 F2 F4
F3
Fn
• Create a file
• Delete a file
• List a directory
• Rename a file
• Naming problem
• Grouping problem
Two-Level Directory
• Separate directory for each user
Path name
Can have the same file name for different user
Efficient searching
No grouping capability
Tree-Structured Directories
Tree-Structured Directories (Cont.)
• Efficient searching
• Grouping Capability
LA/512
Block to be accessed = Q
+ starting address
Displacement into block =
R
Extent-Based Systems
Mapping
Q
LA/511
R
Block to be accessed is the Qth block in the linked chain
of blocks representing the file.
• Indexed allocation
• Each file has its own index block(s) of pointers to its data blocks
• Logical view
index table
Example of Indexed Allocation
Indexed Allocation (Cont.)
• Need index table
• Random access
Q1
LA / (512 x 511)
R1
Q1 = block of index table
R1 is used as follows:
Q2
R1 / 512
R2
Q1
LA / (512 x 512)
R1
More index blocks than can be addressed with 32-bit file pointer
Performance
• Best method depends on file access type
• Contiguous great for sequential and random
• Linked good for sequential, not random
• Declare access type at creation -> select either
contiguous or linked
• Indexed more complex
• Single block access could require 2 index block
reads then data block read
• Clustering can help improve throughput, reduce
CPU overhead
Performance (Cont.)
• Adding instructions to the execution path to save
one disk I/O is reasonable
• Intel Core i7 Extreme Edition 990x (2011) at 3.46Ghz
= 159,000 MIPS
• http://en.wikipedia.org/wiki/Instructions_per_second
• Typical disk drive at 250 I/Os per second
• 159,000 MIPS / 250 = 630 million instructions during one
disk I/O
• Fast SSD drives provide 60,000 IOPS
• 159,000 MIPS / 60,000 = 2.65 millions instructions during
one disk I/O
Free-Space Management
• File system maintains free-space list to track available blocks/clusters
• (Using term “block” for simplicity)
• Bit vector or bit map (n blocks)
0 1 2 n-1
…
1 block[i] free
bit[i] =
0 block[i] occupied
• Grouping
• Modify linked list to store address of next n-1 free
blocks in first free block, plus a pointer to next block
that contains free-block-pointers (like this one)
• Counting
• Because space is frequently contiguously used and
freed, with contiguous-allocation allocation, extents,
or clustering
• Keep address of first free block and count of following free
blocks
• Free space list then has entries containing addresses and
counts
Free-Space Management (Cont.)
• Space Maps
• Used in ZFS
• Consider meta-data I/O on very large file systems
• Full data structures like bit maps couldn’t fit in memory -> thousands
of I/Os
• Divides device space into metaslab units and manages
metaslabs
• Given volume can contain hundreds of metaslabs
• Each metaslab has associated space map
• Uses counting algorithm
• But records to log file rather than file system
• Log of all block activity, in time order, in counting format
• Metaslab activity -> load space map into memory in balanced-
tree structure, indexed by offset
• Replay log into that structure
• Combine contiguous free blocks into single entry
I/O System
Overview
• I/O management is a major component of operating
system design and operation
• Important aspect of computer operation
• I/O devices vary greatly
• Various methods to control them
• Performance management
• New types of devices frequent
• Ports, busses, device controllers connect to various
devices
• Device drivers encapsulate device details
• Present uniform device-access interface to I/O subsystem
I/O Hardware
• Incredible variety of I/O devices
• Storage
• Transmission
• Human-interface
• Common concepts – signals from I/O
devices interface with computer
• Port – connection point for device
• Bus - daisy chain or shared direct access
• PCI bus common in PCs and servers, PCI Express
(PCIe)
• expansion bus connects relatively slow devices
• Serial-attached SCSI (SAS) common disk
interface
I/O Hardware (Cont.)
• Controller (host adapter) – electronics that
operate port, bus, device
• Sometimes integrated
• Sometimes separate circuit board (host adapter)
• Contains processor, microcode, private memory, bus
controller, etc.
• Some talk to per-device controller with bus controller,
microcode, memory, etc.
A Typical PC Bus Structure
I/O Hardware (Cont.)
• Fibre channel (FC) is complex controller,
usually separate circuit board (host-bus
adapter, HBA) plugging into bus
• I/O instructions control devices
• Devices usually have registers where device
driver places commands, addresses, and data
to write, or read data from registers after
command execution
• Data-in register, data-out register, status register,
control register
• Typically 1-4 bytes, or FIFO buffer
I/O Hardware (Cont.)
• Devices have addresses, used by
• Direct I/O instructions
• Memory-mapped I/O
• Device data and command registers mapped to
processor address space
• Especially for large address spaces (graphics)
Device I/O Port Locations on PCs (partial)
Polling
• For each byte of I/O
1. Read busy bit from status register until 0
2. Host sets read or write bit and if write copies data into
data-out register
3. Host sets command-ready bit
4. Controller sets busy bit, executes transfer
5. Controller clears busy bit, error bit, command-ready bit
when transfer done
• Step 1 is busy-wait cycle to wait for I/O from
device
• Reasonable if device is fast
• But inefficient if device slow
• CPU switches to other tasks?
• But if miss a cycle data overwritten / lost
Interrupts
• Polling can happen in 3 instruction cycles
• Read status, logical-and to extract status bit, branch if not zero
• How to be more efficient if non-zero infrequently?
• CPU Interrupt-request line triggered by I/O device
• Checked by processor after each instruction
• Interrupt handler receives interrupts
• Maskable to ignore or delay some interrupts
• Interrupt vector to dispatch interrupt to correct handler
• Context switch at start and end
• Based on priority
• Some nonmaskable
• Interrupt chaining if more than one device at same interrupt
number
Interrupt-Driven I/O Cycle
Interrupts (Cont.)
• Interrupt mechanism also used for exceptions
• Terminate process, crash system due to hardware
error
• Page fault executes when memory access error
• System call executes via trap to trigger kernel
to execute request
• Multi-CPU systems can process interrupts
concurrently
• If operating system designed to handle it
• Used for time-sensitive processing, frequent,
must be fast
Latency
• Stressing interrupt management because even
single-user systems manage hundreds or
interrupts per second and servers hundreds of
thousands
• For example, a quiet macOS desktop generated
23,000 interrupts over 10 seconds
Intel Pentium Processor Event-Vector Table
Direct Memory Access
• Used to avoid programmed I/O (one byte at a time) for
large data movement
• Requires DMA controller
• Bypasses CPU to transfer data directly between I/O
device and memory
• OS writes DMA command block into memory
• Source and destination addresses
• Read or write mode
• Count of bytes
• Writes location of command block to DMA controller
• Bus mastering of DMA controller – grabs bus from CPU
• Cycle stealing from CPU but still much more efficient
• When done, interrupts to signal completion