π Lecture 33 β Physical Disk Management
π Overview: This lecture covers the physical components and operation of disk drives, disk scheduling algorithms, and the design of the BSD 4.4 Fast File System (FFS). Understanding these topics is essential because disk I/O is a major bottleneck in computer systems, and file system design must account for physical disk characteristics to achieve good performance.
ποΈ Topics Covered
The lecture begins with disk components and how the operating system interacts with disks, including the logical block interface. It then covers important disk operation facts such as sector atomicity and read-modify-write cycles. Disk scheduling algorithms including FCFS, SSTF, SCAN, and C-SCAN are presented. Key trends in disk technology are discussed, followed by a detailed examination of the BSD 4.4 Fast File System (FFS), including cylinder groups, allocation policies, small file representation, clustering, and consistency and recovery mechanisms.
π Lecture Summary
Disk Components
A hard disk drive consists of several physical components: platters (rigid circular disks), surfaces (the top and bottom of each platter where data is stored), tracks (concentric circles on each surface), sectors (subdivisions of tracks, the smallest unit of read/write), cylinders (all tracks at the same radial position across all platters), the arm (the mechanical assembly that moves the read/write heads), and heads (the devices that read and write data on the surfaces).
Disk Interaction
Specifying disk requests traditionally required detailed information: cylinder number, surface number, track number, sector number, transfer size, and more. Modern disks, however, use a higher-level interface like SCSI (Small Computer System Interface). In this interface, the disk exports its data as a logical array of blocks numbered [0 ... N]. The disk drive itself handles the mapping from logical block numbers to physical cylinder/surface/track/sector locations.
Some useful facts
Disk drives read and write in units of sectors, not individual bytes. To write a single byte, the disk performs a "read-modify-write" operation: it reads the entire sector containing the byte, modifies that specific byte in memory, then writes the entire sector back to disk. If the sector is already cached in memory, the read step is unnecessary.
The sector is the unit of atomicity for disk writes. A sector write is guaranteed to complete entirely, even if a crash occurs in the middle of the operation (the disk has enough momentum to finish writing the sector). Larger atomic units must be synthesized by the operating system, for example through careful ordering of writes.
π‘ Why this matters: These physical characteristics mean that even writing a single byte requires reading and writing an entire sector (typically 512 bytes or 4KB), which has significant performance implications.
Disk Scheduling
Because seeks (moving the disk arm to a different track) are extremely expensive in terms of time (milliseconds), the operating system schedules queued disk requests to minimize seek overhead.
-
FCFS (First-Come, First-Served): Processes requests in the order they arrive. This is reasonable when the load is low but leads to long waiting times for long request queues because the arm may move back and forth across the disk randomly.
-
SSTF (Shortest Seek Time First): Selects the request with the shortest seek distance from the current arm position. This minimizes arm movement and maximizes request rate, but it can unfairly favor requests for the middle blocks of the disk, potentially causing starvation for outer blocks.
-
SCAN (Elevator algorithm): The disk arm moves in one direction, servicing all requests along the way, until it reaches the end of the disk. It then reverses direction and services requests on the way back. This provides fairer service than SSTF.
-
C-SCAN (Circular SCAN): Similar to SCAN, but the arm only services requests in one direction (like a typewriter carriage). After reaching one end, it quickly returns to the beginning without servicing requests, then services requests again in the forward direction. This provides more uniform wait times than SCAN.
Some useful trends
Disk bandwidth and cost per bit have been improving exponentially, similar to CPU speed and memory size. However, seek time and rotational delay have been improving very slowly because they require moving physical objects (the disk arm and platters).
Implications of these trends:
- Disk accesses are a huge system bottleneck that is getting worse relative to other components.
- Bandwidth increases allow systems to prefetch large chunks of data for about the same cost as reading a small chunk. Performance improves if related data can be read together.
- Cluster related data together on disk to take advantage of this bandwidth.
- Memory size is increasing faster than typical workload sizes, so more and more of the workload fits in the file cache. This shifts disk traffic toward writes and new data rather than reads of existing data.
BSD 4.4 Fast File system (FFS)
The BSD 4.4 Fast File System (FFS) uses a minimum disk block size of 4096 bytes (4KB). The block size is recorded in the superblock, which is the file system's metadata. Multiple file systems with different block sizes can co-reside on the same disk. FFS improves performance through several mechanisms, and the superblock is replicated across multiple locations to provide fault tolerance.
FFS Cylinder Groups
FFS defines cylinder groups as the unit of disk locality. A typical disk has thousands of cylinders divided into dozens of cylinder groups. The allocation strategy is to place "related" data blocks in the same cylinder group whenever possible, because seek latency is proportional to seek distance.
Specific allocation strategies:
- Large files are "smeared" across multiple cylinder groups, with a run of contiguous blocks placed in each group.
- Inode blocks are reserved in each cylinder group, allowing inodes to be allocated close to their directory entries and close to their data blocks (especially important for small files).
π‘ Why this matters: By keeping related data in the same cylinder group, FFS reduces seek distances and improves overall file system performance.
FFS Allocation Policies
-
Allocate file inodes close to their containing directories.
- For mkdir (creating a directory), select a cylinder group with a more-than-average number of free inodes.
- For creat (creating a file), place the inode in the same cylinder group as the parent directory.
-
Concentrate related file data blocks in cylinder groups.
- Most files are read and written sequentially.
- Place the initial blocks of a file in the same cylinder group as its inode.
- For directory blocks: place adjacent logical blocks in the same cylinder group.
- Logical block n+1 goes in the same group as block n.
- Switch to a different cylinder group for each indirect block (a block that contains pointers to other data blocks).
Representing Small Files
Internal fragmentation (wasted space within a file system block) can waste significant space for small files. FFS solves this problem by allowing blocks to be subdivided into fragments (typically 2, 4, or 8 fragments per block). This reduces wasted space for small files while maintaining the performance benefits of large blocks for large files.
Clustering in FFS
Clustering improves bandwidth utilization for large files that are read and written sequentially. FFS can allocate contiguous runs of blocks "most of the time" on disks that have sufficient free space. By reading these contiguous blocks in a single operation, the system maximizes disk bandwidth.
FFS consistency and recovery
When the system reboots after a crash, FFS reconstructs the free list (list of free blocks) and reference counts (number of directory entries pointing to each inode). The file system enforces two invariants:
- Directory names always reference valid inodes.
- No block is claimed by more than one inode.
These invariants are maintained through three ordering rules:
- Write a newly allocated inode to disk before the entry is added to a directory (otherwise a crash could leave a directory pointing to an invalid inode).
- Remove the directory name before the inode is deallocated.
- Write the deallocated inode to disk before its blocks are placed on the free list.
File creation and deletion each require two synchronous writes to disk. The third rule is necessary because otherwise, during inode recovery, a crash could leave the system thinking a deallocated inode still owns blocks that have been placed on the free list.
π‘ Why this matters: These ordering rules ensure file system consistency even in the face of crashes, without requiring a full journaling mechanism.
FFS: inode recovery
Files can be lost if the directory containing them is destroyed or if a crash occurs before a directory link can be set. However, FFS can find lost inodes because:
- FFS pre-allocates inodes in known locations on disk.
- Free inodes are initialized to all zeros.
Using these facts, the fsck (file system check) utility can:
- Find all inodes (whether or not there are any pointers to them) because they're in known locations.
- Determine that any inode with non-zero contents is probably still in use.
- Place unreferenced inodes (those with no directory entry pointing to them but with non-zero contents) in the lost+found directory, allowing the user to examine and recover them.
β Key Takeaways
The physical disk drive has components including platters, surfaces, tracks, sectors, and cylinders; seeks are the main performance bottleneck. Disk scheduling algorithms like FCFS, SSTF, SCAN, and C-SCAN trade off fairness, throughput, and starvation prevention. Disk bandwidth improves exponentially but seek times improve very slowly, making disks an increasing system bottleneck. The Berkeley Fast File System uses cylinder groups to localize related data, large blocks with fragmentation for small files, clustering for sequential access, and careful ordering rules for crash recovery without journaling. FFS can recover lost files by scanning pre-allocated inode locations and checking for non-zero contents.
π§ Quick Revision Questions
- What is the "read-modify-write" operation and why is it necessary for writing a single byte to disk?
- How does the C-SCAN disk scheduling algorithm differ from the SCAN (elevator) algorithm?
- What are cylinder groups in FFS, and what is the primary strategy for allocating related data within them?
- What three ordering rules does FFS use to ensure file system consistency and why is the third rule necessary?
- How does FFS recover lost inodes after a crash, and where does it place unreferenced inodes with non-zero contents?
π Lecture 34 β Log Structured File Systems
π Overview: This lecture introduces log-structured (journaling) file systems, which record each update as a transaction to enable fast crash recovery. It covers logging implementation, log management, performance considerations, and then extends the discussion to Linux Virtual File System (VFS) and the Sun Network File System (NFS) architecture.
ποΈ Topics Covered
The lecture begins with log-structured file systems and the concept of logging for crash recovery, including write-ahead logging implementation. It discusses log management through checkpoint operations and the performance issue of two disk writes per change. It then transitions to Linux Virtual File System (VFS) describing its primary objects: superblock, inode, dentry, and file objects. Finally, it covers the Sun Network File System (NFS) architecture and presents a schematic view of NFS.
π Lecture Summary
Log Structured File Systems
Log structured (or journaling) file systems record each update to the file system as a transaction. All transactions are written to a log. A transaction is considered committed once it is written to the log. However, the file system may not yet be updated β meaning the log holds the record of pending changes.
π‘ Why this matters: This separation allows the system to commit changes quickly to the log (without waiting for the entire file system update) and then apply them later, which improves both performance and reliability during crashes.
π Definition β Write-ahead logging (also called journaling): The practice of always writing changes to the log first, before writing them to the actual file system. All reads go to the file system. If a crash occurs, the system can read the log and correct any inconsistencies in the file system.
Logging
The core idea of logging is: keep track of what operations are in progress and use this for recovery. The system keeps a "log" of all operations. Upon a crash, we can scan through the log and find problem areas that need fixing.
Implementation
Implementation involves adding a log area to disk. The sequence of operations:
- Always write changes to log first β called write-ahead logging or journaling
- Then write the changes to the file system
- All reads go to the file system (not the log)
- Crash recovery β read log and correct any inconsistencies in the file system
Issue - Log management
Observation: The log is only needed for crash recovery.
Checkpoint operation β make in-memory copy of file system (file cache) consistent with disk. After a checkpoint, can truncate the log and start again. Most logging file systems only log metadata (file descriptors and directories) and not file data to keep log size down.
Issue β Performance
There are two key performance concerns:
- Two disk writes (on different parts of the disk) for every change
- Synchronous writes on every file system change
Observation: Log writes are sequential on disk, so even synchronous writes can be fast. Best performance is achieved if the log is on a separate disk.
Current trend is towards logging FS
Benefits include:
- Fast recovery: recovery time is O(active operations) and not O(disk size)
- Better performance if changes need to be reliable
- Sequential synchronous writes are much faster than non-sequential ones
- Examples: Windows NTFS, Veritas on Sun
- Many competing logging file systems for Linux
Linux Virtual File System
The Linux Virtual File System (VFS) provides a uniform file system interface to user processes. It represents any conceivable file system's general feature and behavior. VFS assumes files are objects that share basic properties regardless of the target file system.
Primary Objects in VFS
The VFS is built around four primary objects:
- Superblock object β Represents a specific mounted file system
- Inode object β Represents a specific file
- Dentry object β Represents a specific directory entry
- File object β Represents an open file associated with a process
The Sun Network File System (NFS)
NFS is an implementation and a specification of a software system for accessing remote files across LANs (or WANs). The implementation is part of the Solaris and SunOS operating systems running on Sun workstations using an unreliable datagram protocol (UDP/IP protocol and Ethernet).
Schematic View of NFS Architecture
The architecture shows how NFS enables remote file access across networks, with clients and servers communicating over a network protocol (UDP/IP over Ethernet).
β Key Takeaways
Log-structured file systems use write-ahead logging to record transactions, allowing fast crash recovery with recovery time proportional to active operations rather than disk size. The log is managed through checkpoint operations that synchronize memory with disk, after which the log can be truncated; most implementations log only metadata to keep log size manageable. Performance benefits come from sequential synchronous writes, especially when the log is on a separate disk, as seen in Windows NTFS and Veritas on Sun. The Linux Virtual File System provides a uniform interface through four object types (superblock, inode, dentry, file) that abstract any file system's features. The Sun NFS extends these concepts to remote file access across networks using UDP/IP over Ethernet.
π§ Quick Revision Questions
- What is write-ahead logging and why is it essential for crash recovery in journaling file systems?
- What is the checkpoint operation and what happens to the log after a checkpoint?
- Why do most logging file systems only log metadata and not file data?
- Name the four primary objects in the Linux Virtual File System and state what each represents.
- What protocol does Sun NFS use for communication across LANs or WANs?
π Lecture 35 β Overview of todayβs lecture
π Overview: This lecture covers the principles and software layers of I/O systems in operating systems. It explains the goals of I/O software, different I/O methods (programmed, interrupt-driven, DMA), and how device controllers work. Understanding these concepts is essential for designing efficient and device-independent operating systems.
ποΈ Topics Covered
The lecture covers the goals of I/O software including device independence, uniform naming, error handling, synchronous vs. asynchronous transfers, and buffering. It then explains device controllers, layers of the I/O system, memory-mapped vs. direct I/O, programmed I/O, interrupt-driven I/O, and direct memory access (DMA). Finally, it demonstrates reading a disk sector step-by-step and printing using DMA.
π Lecture Summary
Goals of I/O Software
The goals of I/O software are designed to make I/O operations efficient and easy to use. Device independence means programs can access any I/O device (floppy, hard drive, or CD-ROM) without specifying the device in advance. Uniform naming ensures that the name of a file or device is a string or an integer that does not depend on which machine is used. Error handling should be performed as close to the hardware as possible. Synchronous vs. asynchronous transfers distinguish between blocked transfers and interrupt-driven transfers. Buffering is necessary because data coming off a device cannot be stored in the final destination immediately.
Device Controllers
I/O devices have two main components: a mechanical component and an electronic component. The electronic component is the device controller, which may be able to handle multiple devices. A controller's tasks include converting a serial bit stream to a block of bytes, performing error correction as necessary, and making data available to main memory.
Layers of the I/O system and the main functions of each layer
The I/O system is organized in layers, with each layer having specific functions. The top layer is user-level I/O software, followed by device-independent operating system software, then device drivers, and finally interrupt handlers at the bottom. Each layer provides services to the layer above it and hides implementation details from it.
Memory mapped I/O Vs Direct I/O
Memory mapped I/O means the device controller's registers and internal memory are directly mapped into the processor's address space. In Direct I/O, the device controller's registers and internal memory are accessible via special instructions in the assembly language instruction set. The arguments to these instructions are usually called ports.
Programmed I/O
Programmed I/O (also called polled I/O) involves the CPU directly controlling I/O operations. The CPU continuously checks the device status (polling) until the operation completes. Writing a string to the printer using programmed I/O requires the CPU to output each character one at a time, waiting for the printer to be ready before sending the next character. π Example: Writing a string to the printer using programmed I/O: The CPU loops through each character, writes it to the printer's data register, then waits in a busy loop until the printer signals it is ready for the next character.
Interrupt-Driven I/O
Interrupt-driven I/O improves efficiency over programmed I/O by allowing the CPU to do other work while waiting for I/O operations. When a device completes an operation, it sends an interrupt signal to the CPU. A special interrupt service procedure handles the interrupt and continues the I/O operation. π Example: Writing a string to the printer using interrupt-driven I/O: (a) Code executed when print system call is made initiates the first character transfer and then returns control to the calling program. (b) Interrupt service procedure is called each time the printer finishes printing a character, and this procedure sends the next character.
Reading a Disk Sector: Step 1
The CPU initiates a disk read by writing a command, logical block number, and destination memory address to a port (address) associated with the disk controller.
Reading a Disk Sector: Step 2
The disk controller reads the sector and performs a direct memory access (DMA) transfer into main memory. The DMA controller handles the data transfer directly between the device and memory without CPU intervention.
Reading a Disk Sector: Step 3
When the DMA transfer completes, the disk controller notifies the CPU with an interrupt. The controller asserts a special "interrupt" pin on the CPU to signal completion.
Direct Memory Access (DMA)
Direct Memory Access (DMA) is a hardware mechanism that allows I/O devices to transfer data directly to/from main memory without involving the CPU for each byte. A DMA controller manages the transfer, and the CPU is only involved at the beginning (to set up the transfer) and at the end (to handle the interrupt when transfer completes). π‘ Why this matters: DMA dramatically reduces CPU overhead for large data transfers, allowing the CPU to perform other tasks while data is being moved between devices and memory.
I/O Using DMA
I/O using DMA involves three phases: setup, data transfer, and completion. In the setup phase, the CPU tells the DMA controller the source, destination, and size of the transfer. The DMA controller then performs the data transfer independently. When finished, the DMA controller sends an interrupt to the CPU. π Example: Printing a string using DMA: (a) Code executed when the print system call is made sets up the DMA transfer with the string's memory address and length. (b) Interrupt service procedure is called when the entire string has been printed, notifying the application that the print operation is complete.
β Key Takeaways
The most critical concepts from this lecture are the three main I/O methods (programmed, interrupt-driven, and DMA) and their trade-offs. Programmed I/O is simple but wastes CPU time polling. Interrupt-driven I/O improves efficiency by freeing the CPU during I/O operations. DMA is the most efficient for large transfers as it minimizes CPU involvement. The layers of I/O software provide device independence and uniform naming. Memory-mapped I/O and direct I/O are two different approaches to accessing device controller registers. Error handling should be done as close to the hardware as possible, and buffering is essential when data arrival rate doesn't match consumption rate.
π§ Quick Revision Questions
- What are the five goals of I/O software discussed in this lecture?
- How does programmed I/O (polled I/O) differ from interrupt-driven I/O?
- What are the three steps involved in reading a disk sector using DMA?
- What is the difference between memory-mapped I/O and direct I/O (port-mapped I/O)?
- In the layers of the I/O system, which layer is responsible for handling interrupts from hardware devices?
π Lecture 36 β Device Independent I/O, Kernel Subsystem, and I/O Lifecycle
π Overview: This lecture examines the layered architecture of operating system I/O, focusing on the device-independent software layer that provides a uniform interface to device drivers. It covers buffering strategies, block versus character versus network devices, the kernel I/O subsystem including scheduling and caching, and walks through the complete lifecycle of I/O and network requests.
ποΈ Topics Covered
The lecture covers device-independent I/O software and its functions including buffering and error reporting; classification of devices into block, character, and network categories; the kernel I/O subsystem components such as scheduling, buffering, caching, spooling, and device reservation; error handling and kernel data structures; performance improvement techniques; and the detailed lifecycle of a typical I/O request and a network I/O request.
π Lecture Summary
Device-Independent I/O Software
This layer sits above device drivers and provides a uniform interfacing for all device drivers. It handles buffering, error reporting, allocating and releasing dedicated devices, and providing a device-independent block size.
- Typically interfaces to device drivers through a standard interface
- Offers several buffering options:
- Unbuffered input
- Buffering in user space
- Buffering in the kernel followed by copying to user space
- Double buffering in the kernel
π‘ Why this matters: The device-independent layer allows programmers to write I/O code that works across many different hardware devices without modification.
Block and Character Devices
Block devices include disk drives:
- Commands include read, write, seek
- Support raw I/O or file-system access
- Memory-mapped file access is possible
Character devices include keyboards, mice, serial ports:
- Commands include get, put
- Libraries layered on top allow line editing
π‘ Why this matters: Block devices allow random access to fixed-size data blocks, while character devices deal with streams of bytes; the OS handles them differently.
Network Devices
Network devices are varying enough from block and character to have their own interface. Unix and Windows NT/9i/2000/XP include a socket interface that:
- Separates network protocol from network operation
- Includes select functionality
Approaches vary widely and include pipes, FIFOs, streams, queues, mailboxes.
Clocks and Timers
These provide current time, elapsed time, timer functionality. A programmable interval timer is used for timings and periodic interrupts. The ioctl system call (on UNIX) covers odd aspects of I/O such as clocks and timers.
Blocking and Nonblocking I/O
- Blocking - process suspended until I/O completed, easy to use but insufficient for some needs
- Nonblocking - I/O call returns as much as available, used for user interface and data copy (buffered I/O), implemented via multi-threading inside the kernel, returns quickly with count of bytes read or written
- Asynchronous - process runs while I/O executes, difficult to use, I/O subsystem signals process when I/O completed
Kernel I/O Subsystem
- Scheduling: Some I/O request ordering via per-device queue, some OSs try fairness
- Buffering: Store data in memory while transferring between devices to:
- Cope with device speed mismatch
- Cope with device transfer size mismatch
- Maintain "copy semantics"
- Caching: Fast memory holding copy of data, always just a copy, key to performance
- Spooling: Hold output for a device from multiple sources, used when device can serve only one request at a time (e.g., printing)
- Device reservation: Provides exclusive access to a device, uses system calls for allocation and deallocation, watch out for deadlock
Error Handling
The OS can recover from disk read, device unavailable, and transient write failures. Most return an error number or code when I/O request fails. System error logs hold problem reports.
Kernel Data Structures
The kernel keeps state info for I/O components, including:
- Open file tables
- Network connections
- Character device state
Many complex data structures track buffers, memory allocation, and "dirty" blocks. Some systems use object-oriented methods and message passing to implement I/O.
Unix I/O Kernel Structure
The structure shows a layered architecture: User processes access kernel via system calls, which go through a file system layer, buffer cache, character device layer, device driver, and finally to hardware.
Improving Performance
- Reduce number of context switches
- Reduce data copying
- Reduce interrupts by using large transfers, smart controllers, polling
- Use DMA
- Balance CPU, memory, bus, and I/O performance for highest throughput
Life Cycle of An I/O Request
A typical I/O request goes through:
- User process makes a system call (e.g.,
read()) - Kernel checks parameters, processes request through VFS (Virtual File System)
- VFS dispatches to appropriate file system or device driver
- Device driver issues commands to hardware (disk controller, network card, etc.)
- I/O completes, device generates interrupt
- Interrupt handler processes completion, wakes waiting process
- Data transferred (via DMA or programmed I/O) to user buffer
- System call returns to user process
Inter-computer Communications
This brief section covers how I/O extends to network communications between computers, relying on the socket interface and layered network protocols.
β Key Takeaways
The device-independent I/O software layer provides critical uniformity across different hardware devices by handling buffering, error reporting, and device allocation. Students must understand the distinction between block devices (random access, disks) and character devices (stream-oriented, keyboards/terminals) and how network devices require their own socket-based interface. The kernel I/O subsystem involves scheduling, buffering, caching, spooling, and device reservationβeach solving specific performance or correctness problems. Blocking, nonblocking, and asynchronous I/O represent three different models for process-device interaction that affect system design. Finally, the complete lifecycle of an I/O request from system call through VFS, device driver, interrupt handling, and data transfer to the user buffer is essential for understanding how operating systems manage hardware.
π§ Quick Revision Questions
- What are the four buffering options provided by the device-independent I/O software layer?
- Explain the key differences between block devices and character devices, including typical commands for each.
- What is the difference between blocking, nonblocking, and asynchronous I/O, and when is each appropriate?
- List the five major components of the kernel I/O subsystem and briefly describe the purpose of each.
- Trace the lifecycle of a typical disk read request from user system call to data arrival in user space.
π Lecture 37 β Overview of todayβs lecture
π Overview: This lecture examines Linux interrupt handling mechanisms, distinguishing between interrupts and exceptions at the hardware and software levels. It then covers timing and timer devices, including kernel dynamic timers and user-mode interval timers, which are critical for scheduling, timeouts, and system accounting.
ποΈ Topics Covered
The lecture covers interrupt handlers, the distinction between interrupts (maskable/nonmaskable) and exceptions (faults, traps, aborts, programmed exceptions), Linux interrupt handling components (top-half/bottom-half, tasklets), timing hardware (RTC, PIT, TSC, local APIC timer), dynamic kernel timers (timer_list structure), and user-mode interval timers (setitimer() with ITIMER_REAL, VIRT, PROF).
π Lecture Summary
Interrupt Handlers
Interrupt handlers are best hidden from normal program flow. The interrupt procedure does its task transparently, allowing the interrupted code to resume unaware of the interruption.
Interrupts vs Exceptions
Terminology varies, but for Intel architecture interrupts and exceptions are distinct. Interrupts are device-generated and come in two types: maskable interrupts associated with IRQs (interrupt request lines) that may be temporarily disabled while remaining pending, and nonmaskable interrupts for some critical hardware failures. Exceptions are processor-detected and include faults (correctable/restartable, e.g., page fault), traps (no reexecution needed, e.g., breakpoint), and aborts (severe error; process usually terminated by signal). Additionally, programmed exceptions (software interrupts) include int (system call), int3 (breakpoint), into (overflow), and bounds (address check).
Interrupts and Exceptions
Hardware support is required for getting the CPU's attention. Interrupts often transfer execution from user mode to kernel mode. Asynchronous interrupts are device or timer generated, while synchronous interrupts are the immediate result of the last instruction executed. Intel terminology and hardware includes: IRQs, vectors, IDT (Interrupt Descriptor Table), gates, PIC (Programmable Interrupt Controller), and APIC (Advanced Programmable Interrupt Controller).
Interrupt Handling
Interrupt handling is more complex than exception handling because it requires registry, deferred processing, and other steps. One key issue is that IRQs are often shared; all handlers (ISRs) are executed, so they must query the device to determine if it generated the interrupt. Three types of actions exist: Critical actions (top-half, interrupts disabled briefly), Non-critical actions (top-half, interrupts enabled), and Non-critical deferrable actions (done "later" with interrupts enabled).
π‘ Why this matters: This three-tier separation ensures minimal interrupt latency while allowing non-urgent work to be postponed, which is fundamental to Linux's real-time and throughput performance.
Timing and Timers
Accurate timing is crucial for many OS aspects: Device-related timeouts, file timestamps (created, accessed, written), time-of-day (gettimeofday()), high-precision timers (for code profiling), and scheduling/cpu usage accounting. Intel timer hardware includes: RTC (Real Time Clock), PIT (Programmable Interrupt Timer), TSC (TimeStamp Counter, a cycle counter), and Local APIC Timer (per-CPU alarms). Timer implementations are of two types: kernel timers (dynamic timers) and user "interval" timers (alarm(), setitimer()).
π Definition β Dynamic timers: Timers that may be dynamically created and destroyed with no limit on the number of currently active dynamic timers.
A dynamic timer is stored in the timer_list structure:
struct timer_list {
struct list_head list;
unsigned long expires;
unsigned long data;
void (*function)(unsigned long);
};
Software timers in Linux
Each timer contains a field indicating how far in the future the timer should expire. This field is initially calculated by adding the right number of ticks to the current value of jiffies. The field does not change after initialization. Every time the kernel checks a timer, it compares the expiration field to the value of jiffies at the current moment, and the timer expires when jiffies is greater than or equal to the stored value.
π Formula: expires = jiffies + number_of_ticks β The timer will fire when jiffies >= expires.
π Example: If jiffies is currently 1000 and you want a timer to expire in 50 ticks, you set expires = 1000 + 50 = 1050. The kernel checks this field periodically; when jiffies reaches 1050 or greater, the timer's function is called.
User Mode Interval Timers
The setitimer() system call provides 3 distinct user-mode interval timers that can be set for one-time or periodic alarms, sending signals on timer expiry. The three timers are: ITIMER_REAL (elapsed real time, sends SIGALRM), ITIMER_VIRT (user CPU time, sends SIGVTALRM), and ITIMER_PROF (user + kernel CPU time, sends SIGPROF). Implementations differ: VIRT and PROF are updated by PIT or APIC interrupts. REAL requires a kernel timer and may need to deliver to a blocked process, using current->real_timer. The REAL timer is shared by the alarm() API, so you cannot use both simultaneously.
π‘ Why this matters: Understanding these timers is essential for writing user programs that need precise timeouts, profiling, or periodic signal delivery, and for understanding how the OS manages time-sharing and resource accounting.
β Key Takeaways
You must remember the fundamental distinction between interrupts (device-generated, asynchronous) and exceptions (processor-detected, synchronous) including their subtypes: maskable/nonmaskable interrupts and faults/traps/aborts. For interrupt handling, recall the three-tier architecture (critical, non-critical, deferrable) and that shared IRQs require all handlers to query devices. For timers, know the four Intel timer hardware types (RTC, PIT, TSC, Local APIC) and that dynamic kernel timers use the timer_list structure with expires, data, and function fields compared against jiffies. Finally, remember the three user-mode interval timers (ITIMER_REAL, ITIMER_VIRT, ITIMER_PROF) and their associated signals, with the key implementation detail that ITIMER_REAL uses current->real_timer shared with alarm().
π§ Quick Revision Questions
- What is the difference between a maskable interrupt and a nonmaskable interrupt?
- Name the three types of processor-detected exceptions and give one example for each.
- Why must all interrupt handlers in a shared IRQ line execute and query the device?
- What are the three fields in the
timer_liststructure, and how does the kernel determine when a dynamic timer expires? - Explain the difference between
ITIMER_REAL,ITIMER_VIRT, andITIMER_PROFβ which one cannot be used simultaneously withalarm()and why?
π Lecture 38 β Loadable Kernel modules and device drivers; Signals and asynchronous event notification
π Overview: This lecture covers two major topics. First, it explains the Linux and Solaris loadable kernel module system, including module management, driver registration, and conflict resolution. Second, it provides a comprehensive introduction to signals as an asynchronous event notification mechanism in Unix/Linux, covering their APIs, semantics, and the evolution from old unreliable signals to modern reliable and real-time signals.
ποΈ Topics Covered
The lecture begins with an overview of Loadable Kernel Modules in Linux and Solaris, explaining their purpose and the three core components: module management, driver registration, and conflict resolution. It then transitions to a detailed discussion of Signals, covering them as an early minimal IPC mechanism, their three distinct APIs, basic properties like names and numbers, generation sources, pending signals, system calls, default actions, and a specific comparison between old unreliable signals and modern reliable signals via the sigaction() call.
π Lecture Summary
Loadable Kernel Modules (Linux & Solaris)
These are sections of kernel code that can be compiled, loaded, and unloaded independent of the rest of the kernel. A kernel module may typically implement a device driver, a file system, or a networking protocol. The module interface allows third parties to write and distribute device drivers or file systems on their own terms, even if they could not be distributed under the GPL. Kernel modules allow a Linux system to be set up with a standard, minimal kernel, without any extra device drivers built in. There are three components to Linux module support: module management, driver registration, and conflict resolution.
Module Management
This component supports loading modules into memory and letting them talk to the rest of the kernel. Module loading is split into two separate sections: managing sections of module code in kernel memory, and handling symbols that modules are allowed to reference. The module requestor manages loading of requested but currently unloaded modules; it also regularly queries the kernel to see whether a dynamically loaded module is still in use and will unload it when it is no longer actively needed.
Driver Registration
This component allows modules to tell the rest of the kernel that a new driver has become available. The kernel maintains dynamic tables of all known drivers and provides a set of routines to allow drivers to be added to or removed from these tables at any time. Registration tables include the following items: device drivers, file systems, network protocols, and binary format.
Conflict Resolution
This is a mechanism that allows different device drivers to reserve hardware resources and to protect those resources from accidental use by another driver. The conflict resolution module aims to prevent modules from clashing over access to hardware resources, prevent autoprobes from interfering with existing device drivers, and resolve conflicts with multiple drivers trying to access the same hardware.
Signals
Signals are an early minimal IPC (no information) mechanism that functions as an asynchronous event notification system and a software analog of hardware interrupts. There are three distinct APIs: the original (buggy, unreliable) signals with slightly differing semantics between Sys V and BSD, reliable (Posix) signals, and real-time (Posix) signals. Things you can do with signals include: generate (send, raise) using kill(), deliver (receive, handle) during kernel to user transition, block/mask to temporarily disable delivery (but not generation), ignore to throw away on delivery, and catch (handle) to execute a user-supplied handler on delivery.
Signals: Basics
Signals have names (macros) and numbers. Examples include SIGINT (2), SIGKILL (9), and SIGPWR (30). The kill βl command lists platform assignments. Some signals are architecture and processor dependent, such as SIGSTKFLT for coprocessor stack error on Intel. Signals can be generated by users via special shell characters (control-c) or user-level commands (kill -9 1234), by programs via system calls (kill(pid, sig)), or by the kernel (e.g., in response to exceptions). In Linux, regular signals are numbers 1-31 (assigned specific functions), and real-time signals are numbers 32-64 (user assignable).
Pending signals are generated but not delivered, and they may be blocked or not-blocked. Regular signals "can't count" β generation of an already pending signal is not recorded; think of it as a single bit that is "set" on generation. In contrast, realtime signals "queue" as a linked list of generated signals (up to some maximum).
Basic system calls include: kill() and rt_sigqueueinfo() for generation; sigprocmask() and rt_sigprocmask() for blocking and unblocking; sigpending() and rt_sigpending() to check pending signals; sigaction(), signal(), and rt_sigaction() to establish a handler; and sigsuspend() and rt_sigsuspend() to wait for a signal. Calls often operate on signal sets, which are two-element arrays of ints (64-bit bitmask).
Regarding blocking, pending, and delivery: blocked signals have delivery delayed until unblocked, and it is possible for a signal to be blocked with no signal pending. A generated signal is pending for a short while even if unblocked. Unblocked pending signals are delivered on kernel to user transition, with delivery opportunities every timer interrupt (but only for current). When masking signals, current signal delivery is masked during handler execution (like interrupt masking), so handlers need not be re-entrant. Old, buggy semantics did not mask the current signal.
All signals have a default action: terminate, dump (terminate and dump core), ignore (throw away on delivery), stop (control-z), or continue. It is possible to catch most signals by establishing a user-specified handler. However, SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
Signals: Old Unreliable Signals
The old call signal() was unreliable and buggy because further signal delivery was not masked during the handler, and the default action was restored on delivery. The common programming idiom was to re-establish the handler inside the handler:
myhandler() {
// window of vulnerability here!
signal(SIGWHATEVER, myhandler) // reestablish
// do something to handle signal
}
The consequence is that a new delivery can occur during the window of vulnerability, making it impossible to reliably catch all signals. The new call sigaction() is reliable: it avoids the problem because signals are masked and there is no reset. It is parameterized to make old semantics still available, and signal() just calls sigaction() with appropriate parameters.
Signals: System Calls
The key system calls are:
kill(pid, sig)sigaction(sig, act, oact)(which replacessignal())sigpending()sigprocmask()sigsuspend()
β Key Takeaways
Loadable kernel modules are sections of kernel code that can be dynamically loaded and unloaded, independent of the rest of the kernel, and they rely on three components: module management, driver registration, and conflict resolution. Conflict resolution is essential for preventing multiple drivers from clashing over hardware resources. Signals are an asynchronous notification mechanism, like software interrupts, with three API generations: old unreliable, reliable Posix, and real-time Posix. The critical difference between old and reliable signals is that sigaction() masks further delivery during handler execution and does not reset the default action, solving the window of vulnerability problem in signal(). SIGKILL and SIGSTOP are the two signals that cannot be caught, blocked, or ignored.
π§ Quick Revision Questions
- What are the three components to Linux module support?
- What is the purpose of the conflict resolution module?
- What are the three distinct APIs for signals?
- What is the fundamental difference between how regular signals and real-time signals handle multiple pending generations?
- Why is the old
signal()call considered "unreliable," and how doessigaction()fix this?