Operating System I/O and File System Fundamentals
I/O Device Types and Addressing
- Block Devices: Stores information in fixed-size blocks; transfers are in units of whole blocks (e.g., hard drives, SSDs, USB drives).
- Character Devices: Delivers/accepts streams of characters, without regard to block structure; not addressable, no seek operation (e.g., keyboard, mice, serial ports).
I/O Addressing Methods
- Port-Mapped (Isolated I/O): I/O devices have a separate address space. Special instructions (
IN
&OUT
) are used to access devices. - Memory-Mapped I/O: Device registers are mapped into system memory. Uses regular load and store instructions. Simplifies programming but consumes memory address space.
Data Transfer Mechanisms
- Direct Memory Access (DMA): Used to offload data transfers from the CPU. Transfers data directly between memory and I/O devices. The CPU sets up DMA and is interrupted when the transfer is complete. Increases performance, especially for large data transfers.
- Interrupts: Mechanism where hardware signals the CPU for attention, used to avoid busy-waiting in I/O operations.
Interrupt Precision
- Precise Interrupt: Occurs when the CPU can cleanly pause and resume execution because all four conditions are met:
- PC saved in a known place.
- All instructions before that pointed to by PC have fully executed.
- All instructions before that pointed to by PC have fully executed.
- Execution state of instruction pointed to by PC is known.
- Imprecise Interrupt: Does not guarantee all four conditions. May cause ambiguous execution state, making it harder to debug or recover from.
I/O Techniques
- Programmed I/O: CPU polls the device until it is ready. Simple but inefficient (wastes CPU time).
- Interrupt-Driven I/O: Device interrupts the CPU when it is ready. More efficient than polling.
- DMA I/O: CPU sets up the transfer, then DMA handles it. CPU is free during the data transfer.
DMA Process Steps
- CPU sets up the DMA controller and gives it the source address, destination size, transfer size, and direction.
- I/O device signals ready to the DMA controller.
- DMA takes over the bus.
- DMA transfers data from I/O to memory.
- DMA sends an interrupt signal to the CPU when the transfer is complete.
I/O Software Structure and Goals
I/O Software Goals
- Device Independence: Same code works with any device.
- Uniform Naming: All devices named consistently.
- Error Handling: Detect and recover from device failures.
- Synchronization: Synchronous (Sync): Process waits. Asynchronous (Async): Process continues; notified later.
- Buffering: Temporary storage to improve performance and manage speed mismatches.
I/O Software Layers
- User-Level I/O: Applications (like Chrome or Discord) making I/O calls, libraries (like
printf()
,scanf()
, or GUI interfaces). - Device-Independent Software: Translates generic commands (e.g.,
read()
) for all devices. - Interrupt Handlers: Called when a device triggers an interrupt.
- Device Drivers: Communicates directly with hardware; written by hardware vendors. The interface allows plug-and-play functionality.
- Hardware: Actual device components.
Buffering Techniques
- No Buffering: Data transferred directly to/from the user program.
- Single Buffer: Data first goes to a buffer, then processed.
- Double Buffering: Two buffers used to hide delays, often used for smooth playback or streaming.
- Kernel/User Buffering: Data may first go to the kernel, then copied to user space, which can cause performance bottlenecks.
Disk Management and Scheduling
Disk Scheduling Metrics
- Seek Time: Time taken to move the head to the correct track.
- Rotational Delay: Time spent waiting for the desired sector to rotate under the head.
Disk Scheduling Algorithms (ALGS)
- FCFS (First-Come, First-Served): Serve requests in arrival order.
- SSF (Shortest Seek First): Closest request is served first.
- SCAN (Elevator): Moves in one direction, serving requests as it goes.
Stable Storage
Guarantees data persists after crashes. Uses redundant disks (RAID 1), stable writes, acknowledgments, and retries.
Branch Prediction
Algorithms figure out what the next instruction is going to be, then place it somewhere for fast retrieval (improving CPU pipeline efficiency).
X Window System (GUI)
Uses a client-server model. The server manages the display, keyboard, and mouse. Clients/applications send rendering and input requests.
File System Structure and Management
A File System controls how data is stored and retrieved on disk, managing file access, structure, naming, and permissions.
Essential File System Requirements
- Must store large volumes of data.
- Data must persist after the program ends.
- Multiple processes must access data concurrently.
Common File System Types
- HFS+ (Apple): Note: Cannot natively write to NTFS (Windows).
- FAT32: Simple, widely supported, but does not support permissions. Max file size is approximately 2 GB.
- ext4: Default Linux file system. Uses inodes instead of tables.
- ext2: Non-journaling, fast, but unsafe on power loss.
- ext3: Journaling version of ext2.
- NTFS: Windows FS, supports journaling, metadata, and permissions.
Disk Organization and Access
- Disk: A linear sequence of fixed-size blocks.
- Basic Operations:
read(block_k)
andwrite(block_k)
. Failure to fully write can lead to the system becoming unusable. - File Table: Fast access is important because millions of small files would make the table incredibly slow to access.
- Paging File: Accesses the file system to temporarily buffer files; typically not fully used, not optimal.
Fundamental Questions in File Systems
- How to find information?
- How to keep one user from reading another user’s data?
- How to know which blocks are free?
File Organization Types
- Byte Sequence: Stream of bytes (most flexible, used for text files).
- Record Sequence: Fixed-size records, good for databases.
- Tree: Hierarchical structure, often used for XML or configuration files.
Standard File Operations
Create, Delete, Open, Close, Read, Write, Seek, Rename, Append, Get/Set Attributes.
Directory Structures
- Single-Level: All files in one folder. Easy but limited.
- Hierarchical: Tree-like structure, supports subdirectories (UNIX-style).
- Pathnames: Absolute or relative (e.g.,
/home/user/file.txt
).
File Storage Allocation Methods
- Contiguous: All file blocks stored together. Fast but prone to fragmentation.
- Linked List: Each block has a pointer to the next. Easy to grow, but slow for random access.
- FAT (File Allocation Table): Like a linked list, but pointers are stored in a table in memory.
- Inodes: Used in Linux; metadata and block pointers are stored in a separate inode structure.
UNIX File Removal Process
- Remove file entry from the directory.
- Release the inode to the pool of free inodes.
- Return all associated disk blocks to the pool of free disk blocks.
Virtual File System (VFS)
An abstraction layer between the user and the physical File System, allowing support for different file systems under one unified interface.
Disk Space Management
- Free List: A linked list of free blocks. Easy to update but inefficient for large disks.
- Bitmap: Each bit represents a block (0 = free, 1 = used). Fast and compact.
File System Consistency
A file system is consistent if every block is accounted for once, meaning there are no missing or doubly-allocated blocks.
Linux File Permissions
Octal Permission Codes
- 0: No permission
- 1: –x Execute only
- 2: -w- Write only
- 3: -wx Write and execute (1+2)
- 4: r– Read only
- 5: r-x Read and execute (4+1)
- 6: rw- Read and write (4+2)
- 7: rwx Read, write, and execute (4+2+1)
Permission Examples
ls -l
Format: Displays permissions in the format:-: File type (- for regular file, d for directory)
followed byrwx
(Owner permissions),r-x
(Group permissions),r--
(Others’ permissions).rwxrwxrwx
(777): Everyone can read, write, and execute.rwxr-xr-x
(755): Owner has all permissions; group/others have read and execute.r--r--r--
(444): Everyone has read only access.- 700: Owner has all permissions; group and others have none.
- 664: Owner can read/write; group can read/write; others can read.
- 511: Owner can execute/read; group can execute; others can execute.
Kernel Design and Security Vulnerabilities
Modular Kernel Design
Allows drivers to be loaded dynamically without recompiling the kernel (via Plug and Play).
Storage Media Characteristics
- Optical Disk:
- Pros: Cheap, portable, durable.
- Cons: Slow, fragile, low capacity.
- Magnetic Disk:
- Pros: High capacity, fast, rewrite-friendly.
- Cons: Expensive, sensitive to damage.
CPU Performance Features
- Speculative Execution: Modern CPUs predict and execute instructions ahead of time to improve performance.
Side-Channel Attacks
- Meltdown: A critical hardware vulnerability that allows unauthorized access to protected memory areas, breaking the fundamental isolation between user applications and the operating system. It exploits out-of-order execution to read arbitrary kernel-memory locations, including personal data and passwords.
- Spectre: A class of security vulnerabilities affecting modern microprocessors, allowing attackers to trick programs into accessing arbitrary memory. It exploits features like branch prediction and speculative execution. Can leak data from other programs running on the same machine.
Mitigation
Question: What does Kernel Page Table Isolation (KPTI) help mitigate?
Answer: KPTI helps mitigate Meltdown vulnerabilities.