Operating SystemsUnit 612 min read
Device Management – I/O Architecture, Drivers, Scheduling, Buffering & RAID
Unit 6 of Operating Systems: this note explains the fundamentals of device management, covering I/O hardware, communication techniques, driver structure, allocation policies, buffering, disk scheduling, RAID levels and performance considerations, with examples and comparison tables.
Key points
- I/O can be performed by programmed I/O, interrupt‑driven I/O, or DMA, each with distinct trade‑offs.
- Device drivers act as the OS‑hardware interface, providing abstraction, resource management and error handling.
- Buffering, spooling and caching are key techniques to hide device latency and improve throughput.
- Disk scheduling algorithms (FCFS, SSTF, SCAN, C‑SCAN) affect average seek time and overall system performance.
- RAID configurations combine multiple disks for redundancy or speed, and their suitability depends on workload characteristics.
1. Introduction to Device Management
Device management is the OS subsystem that controls all hardware peripherals, from keyboards and printers to magnetic disks and network cards. Its primary responsibilities are
- Abstracting hardware details so that applications can request I/O without knowing device specifics.
- Coordinating access to shared devices, preventing conflicts and ensuring fairness.
- Optimising performance by selecting appropriate I/O techniques, buffering strategies, and scheduling policies.
The device manager works closely with the kernel, file system, and process scheduler. It receives I/O requests from processes (via system calls such as read, write, open), translates them into device‑specific commands, and monitors completion.
2. I/O Hardware and Interfaces
2.1 Basic Components
| Component | Function | Typical Example |
|---|---|---|
| Controller | Interface logic that translates generic commands into device‑specific signals. | Disk controller, USB host controller |
| Device Register Set | Memory‑mapped or port‑mapped registers used for command, status, and data transfer. | UART data register, ATA command register |
| Interrupt Controller | Aggregates interrupt signals from multiple devices and forwards them to the CPU. | PIC (8259), APIC (Advanced PIC) |
| DMA Engine | Performs bulk data transfer between main memory and device without CPU intervention. | PCI‑DMA, SATA DMA engine |
2.2 Bus Architectures
- ISA / PCI / PCI‑Express: Provide address and data lines, support plug‑and‑play enumeration, and allow devices to generate interrupts or DMA requests.
- USB / FireWire: Serial bus with hot‑plug capability, using a host controller driver to manage multiple endpoints.
Understanding the bus is essential because the OS must configure address spaces, IRQ lines, and DMA channels during boot‑time device discovery.
3. I/O Techniques
Three classic techniques are used to move data between the CPU and a device.
| Technique | How it works | CPU involvement | Latency hiding | Typical use |
|---|---|---|---|---|
| Programmed I/O (Polling) | CPU repeatedly reads a status register until the device signals readiness. | High – CPU busy‑waits each byte/word. | None – CPU blocked. | Simple low‑speed devices (e.g., early keyboards). |
| Interrupt‑Driven I/O | Device raises an interrupt when it can accept or provide data; CPU executes an ISR, then resumes the blocked process. | Moderate – ISR runs only when needed. | Good – CPU can perform other work between interrupts. | Serial ports, network cards. |
| Direct Memory Access (DMA) | DMA controller transfers blocks of data directly between memory and device, while CPU is free to execute other tasks. | Low – CPU initiates transfer and is notified on completion. | Excellent – large transfers without CPU overhead. | Disk I/O, high‑speed network adapters, graphics cards. |
3.1 Worked Example: Interrupt‑Driven Keyboard Input
Assume a process calls read(fd, &c, 1) to obtain a single character from the keyboard. The trace below shows the interaction between the process, OS, and hardware.
Process OS Kernel Keyboard Controller
| | |
|--- read(fd, &c,1) --> | |
| |--- check buffer (empty) ----> |
| |<--- buffer empty, block ---- |
| |--- enable IRQ for keyboard -->|
| | |
| (process blocked) | |
| | |
|<--- IRQ arrives (key press) ------------------------|
| |--- ISR: read scan code -----> |
| |--- translate to ASCII -----> |
| |--- place char in buffer ----|
| |--- wake up blocked process -|
|--- return char c ---> | |
| | |
Key points: the CPU is free to run other processes while the keyboard controller waits for a key press. The ISR (Interrupt Service Routine) performs minimal work—reading the scan code, converting it, and waking the blocked process.
4. Device Drivers
A device driver is a kernel module that implements a standard interface (open, close, read, write, ioctl) for a specific class of hardware. Drivers can be character, block, or network drivers, depending on the device type.
4.1 Driver Structure
+---------------------------+
| Device Driver (kernel) |
+---------------------------+
| 1. Initialization |
| 2. Open / Close |
| 3. Read / Write |
| 4. IOCTL (control) |
| 5. Interrupt handler |
| 6. DMA setup (if needed) |
| 7. Cleanup / Unload |
+---------------------------+
- Initialization: Detect hardware, allocate resources (I/O ports, IRQ, DMA channel), register the device node (
/dev/sda). - Open/Close: Manage reference counts, enforce exclusive access if required.
- Read/Write: Translate generic requests into device‑specific commands; may invoke DMA or issue interrupts.
- IOCTL: Provide device‑specific control operations (e.g., setting baud rate on a serial port).
4.2 Driver Models
| Model | Description | Example |
|---|---|---|
| Monolithic driver | Compiled directly into the kernel; high performance, but requires reboot to update. | Traditional Linux block drivers (ext4). |
| Loadable kernel module (LKM) | Can be inserted/removed at runtime; facilitates hot‑plug devices. | usb-storage.ko. |
| User‑space driver | Driver runs in user space, communicating via a kernel proxy (e.g., FUSE). | libusb based drivers, nvidia proprietary driver (partially). |
4.3 Error Handling & Recovery
Drivers must detect hardware faults (e.g., CRC errors, timeout) and propagate them to the OS via error codes (EIO, ENODEV). Recovery strategies include retry, reset, or mark device offline.
5. Device Allocation, Buffering & Spooling
5.1 Allocation Policies
- Pre‑allocation (static) – Resources assigned at boot; simple but inflexible.
- Dynamic allocation – Resources granted on demand; uses data structures like free lists for IRQs, DMA channels, and I/O ports.
The OS maintains a device control block (DCB) for each device, storing its state, allocation status, and pending I/O queues.
5.2 Buffering Techniques
| Technique | When used | Advantages | Disadvantages |
|---|---|---|---|
| Single‑buffer (no buffering) | Real‑time, low‑latency I/O (e.g., terminal). | Minimal memory overhead. | CPU must wait for device; low throughput. |
| Double‑buffering | When producer and consumer can work concurrently (e.g., video playback). | Overlaps I/O with processing; reduces idle time. | Requires twice the buffer memory. |
| Circular (ring) buffer | Stream devices (e.g., network cards). | Continuous flow, easy wrap‑around handling. | Needs careful pointer management to avoid overflow. |
| Spooling | Devices with high latency and batch processing (e.g., printers). | Decouples user processes from device speed; enables job scheduling. | Extra disk space needed; possible delay. |
Worked Example: Circular Buffer for a Serial Port
Assume a circular buffer of size 8 bytes. The ISR writes incoming bytes at head, the user process reads from tail.
Initial: head = 0, tail = 0, buffer empty
ISR receives byte 'A':
buffer[head] = 'A' // buffer[0] = 'A'
head = (head + 1) % 8 = 1
Process reads:
byte = buffer[tail] // byte = 'A'
tail = (tail + 1) % 8 = 1
When head == tail after a write, the buffer is full; the ISR may discard new data or overwrite oldest data based on policy.
5.3 Spooling Workflow
- Application writes print job → OS writes to spool file on disk.
- Spooler daemon reads the file, formats it, and sends to printer driver.
- Printer driver uses interrupt‑driven I/O to feed data to the printer.
Spooling enables multiple users to submit jobs simultaneously while the printer processes them sequentially.
6. Disk Scheduling & RAID
6.1 Disk Scheduling Algorithms
| Algorithm | Principle | Average Seek Time (qualitative) | Starvation? |
|---|---|---|---|
| FCFS (First‑Come‑First‑Served) | Serve requests in arrival order. | Poor (depends on request pattern). | No |
| SSTF (Shortest Seek Time First) | Choose request with minimal head movement. | Better than FCFS, but can cause starvation of far‑away requests. | Yes |
| SCAN (Elevator) | Move head in one direction servicing all requests, then reverse. | Good, more uniform response time. | No |
| C‑SCAN (Circular SCAN) | Like SCAN but on reaching end, head returns quickly without servicing. | Provides more uniform wait time than SCAN. | No |
| LOOK / C‑LOOK | Same as SCAN/C‑SCAN but only go as far as the last request in each direction. | Slightly less movement than SCAN/C‑SCAN. | No |
Example trace (requests: 55, 58, 39, 18, 90; initial head at 50, SCAN moving upward):
Head moves: 50 → 55 → 58 → 90 → (reverse) → 39 → 18
Total movement = (55-50)+(58-55)+(90-58)+(90-39)+(39-18) = 5+3+32+51+21 = 112 cylinders
6.2 RAID Levels
| RAID | Minimum Disks | Fault Tolerance | Performance | Use Cases |
|---|---|---|---|---|
| 0 (Striping) | 2 | None | High read/write (parallel) | Temporary storage, high‑speed cache |
| 1 (Mirroring) | 2 | One disk failure | Good read (can read from either), write penalty | Critical data, small arrays |
| 5 (Striped + Parity) | 3 | One disk failure | Good read, moderate write (parity calc) | General purpose servers |
| 6 (Double Parity) | 4 | Two disk failures | Similar to RAID‑5, extra parity overhead | Large storage, high reliability |
| 10 (Mirror + Stripe) | 4 | One disk per mirrored pair | Excellent read/write | Databases, high‑transaction systems |
Advantages/Disadvantages
- Striping (RAID‑0) gives maximum throughput but no redundancy—suitable only when data can be recreated.
- Mirroring (RAID‑1) doubles storage cost but provides instant failover; write performance suffers due to duplicate writes.
- Parity‑based (RAID‑5/6) balances capacity and reliability; write penalty arises from parity computation and extra I/O.
- Hybrid (RAID‑10) offers best of both worlds at higher cost; ideal for mission‑critical workloads.
7. Performance Evaluation & Metrics
| Metric | Definition | Typical Measurement |
|---|---|---|
| Throughput | Amount of data transferred per unit time (bytes/s). | iostat, dd benchmark. |
| Latency | Time from request issuance to first byte received. | ping for network, hdparm -tT for disks. |
| CPU Utilisation | Percentage of CPU cycles spent handling I/O. | top, vmstat. |
| Interrupt Rate | Number of interrupts per second generated by a device. | /proc/interrupts. |
| DMA Transfer Size | Maximum block size that can be moved without CPU involvement. | Device specification. |
Optimising device management often involves reducing interrupt overhead (e.g., using interrupt coalescing), increasing DMA block size, and choosing appropriate buffering to match device speed differences.
8. Security & Protection in Device Management
- Access control: Device files in
/devhave Unix permissions; only authorised users may open them. - Isolation: Modern OSes use IOMMU (Input‑Output Memory Management Unit) to prevent DMA attacks where a malicious device could write to arbitrary memory.
- Hot‑plug validation: USB devices are enumerated and may be subject to policy checks (e.g.,
udevrules).
Exam tip
- Conceptual questions often ask you to compare I/O techniques; memorise the table of Programmed I/O, Interrupt‑driven I/O, and DMA, focusing on CPU involvement and latency hiding.
- Trace questions: Practice the interrupt‑driven I/O sequence (process → OS → ISR → wake‑up) and be ready to label each step.
- Diagram/short answer: Be able to sketch a simple driver structure and label its main functions (init, open, read/write, ioctl, ISR, cleanup).
- Algorithmic problems: For disk scheduling, calculate total head movement for a given request sequence using SCAN or C‑SCAN; write the steps clearly.
- RAID: Remember the minimum number of disks, fault tolerance, and a single advantage/disadvantage for each RAID level; a quick 2‑column table in your answer earns marks.
Focus on definitions, the flow of an I/O request, and the trade‑offs between techniques—these are the high‑yield topics in Unit 6.
Based on the TU BSc CSIT syllabus for Operating Systems (CSC264), unit 6.
Discussion
Loading…