Chapter 11: Input/Output

Chapter 11: Input/Output · Study Questions
Device classesPollingInterruptsDMABufferingAsynchronous I/OClocksDisk access timeDisk schedulingSSDRAID and parity
Sercan KÜLCÜ

Multiple Choice · Introduction

Question 1: Which Pairing Is Wrong?

Operating systems classify devices as block, character, and network devices. Which of the following pairings is wrong?

C: A mouse produces movement and click events as a stream; it has no addressable blocks and no lseek, so it is a character device (/dev/input/event* on Linux). In a block device each block is read and written independently by its number, and file systems are built on top of them. Network interfaces are a separate class and have no files in /dev.

Multiple Choice · I/O Hardware

Question 2: The Wait Loop That Never Ends

A driver reads the status register of a memory-mapped device with the loop while ((*status & READY) == 0) ; and never leaves the loop although the device is ready. What is the most likely cause?

D: A device register's value changes without the CPU knowing. If the page is marked cacheable, the CPU sees the old cached value; if the pointer is not volatile, the compiler hoists the single read out of the loop. Fix: map the register page uncacheable (ioremap) and use volatile. A memory-mapped register is read with ordinary load instructions; B belongs to port-mapped I/O.

Output Question · Programmed I/O

Question 3: The CPU Cost of Polling

On a 2 GHz CPU, one poll (read status, compare, take the data) takes 400 cycles. If the following devices are monitored by polling alone, what percentage of CPU time goes to it?

  1. A mouse polled 30 times per second so as not to miss movement
  2. A disk controller producing 100 MB/s (1 MB = 10⁶ B) and delivering 16 bytes per poll

Which transfer method do the results call for?

Show answer

Ratio = (polls/s × cycles/poll) / (cycles/s).

(a) 30 × 400 = 12,000 cycles/s → 12,000 / 2 × 10⁹ = 0.0006%: negligible.

(b) 100 × 10⁶ / 16 = 6.25 × 10⁶ polls/s × 400 = 2.5 × 10⁹ cycles/s → 125%: even the whole CPU is not enough.

For a slow, sparse device polling is harmless; for high-speed block transfer, neither polling nor an interrupt per byte is suitable. The solution is DMA: the CPU deals only with setup and the completion interrupt.

Output Question · Programmed I/O

Question 4: The Polling Loop

read_status() behaves like reading a device's status register: bit 2 is “ready”, bit 1 is the “error” bit. What does the program print? What does the CPU do during this time?

#define READY 0x4
#define ERROR 0x2
int status[] = {0x1, 0x3, 0x2, 0x6, 0x4};
int k = 0;
int read_status(void) { return status[k++]; }
int main(void) {
  int waits = 0, d;
  while (((d = read_status()) & READY) == 0)
    waits++;
  printf("waits=%d k=%d", waits, k);
  if (d & ERROR) printf(" error
");
  else printf(" ok
");
  return 0;
}
Show answer

Output: waits=3 k=4 error

  • 0x1 (001), 0x3 (011), 0x2 (010): ready bit (0x4) is 0 → three idle rounds, waits = 3.
  • 0x6 (110): ready bit is 1 → the loop ends; d = 0x6, read_status was called 4 times (k = 4). The last 0x4 is never read.
  • 0x6 & 0x2 = 2 ≠ 0 → “error”.

During this time the CPU only rereads the register (busy waiting); with interrupts or DMA, these rounds could be spent on other work.

Multiple Choice · Interrupts

Question 5: The Order When an Interrupt Arrives

Process A is running when the disk read that B was waiting for finishes. The steps are given in scrambled order:
P: Hardware saves PC and PSW on the kernel stack and switches to kernel mode.
Q: The disk sends an interrupt request; the CPU finishes the current instruction.
R: The vector number indexes the IDT, and the handler address is found.
S: The handler acknowledges the device (EOI) and moves B to the ready queue.
T: The scheduler picks a process, and iret returns to user mode.
Which is the correct order?

B: First the device requests an interrupt and the CPU stops at an instruction boundary (Q). Hardware saves the state needed for the return (P) and finds the handler through the vector (R). The handler is kept short: it acknowledges the device and makes the waiting B ready (S). Finally the scheduler picks A or B and iret returns (T). C jumps to the handler without saving state; the return address would be lost.

Output Question · DMA

Question 6: An Interrupt per Sector or DMA?

1 MiB of data is read from a disk controller.

  1. With the interrupt-driven method, one interrupt arrives for each 512-byte sector; the handler copies the sector into memory and spends 4 µs in total.
  2. With DMA, the CPU does the setup in 6 µs; at the end of the transfer, a single 4 µs interrupt arrives.

Compare the times the CPU spends on this read. When does DMA not pay for itself?

Show answer

(a) 1 MiB = 1,048,576 B → 1,048,576 / 512 = 2048 interrupts × 4 µs = 8192 µs (about 8.2 ms).

(b) 6 + 4 = 10 µs. The CPU load drops about 800-fold. During the transfer, DMA uses the bus now and then, so the CPU's memory accesses may wait a little (cycle stealing), but that is small next to the copying cost.

For transfers of a few bytes, the fixed setup cost outweighs the gain; if the device is very slow or the CPU has nothing else to do, interrupt-driven I/O or polling can be simpler and sufficient.

Multiple Choice · DMA

Question 7: What Is Cycle Stealing?

Which statement best describes “cycle stealing” in the context of DMA?

A: Cycle stealing is a DMA operating mode: for each word the controller takes the bus for a few cycles; the CPU does not stop completely, it only slows a little when it accesses memory. The opposite is burst mode: the bus is held for a whole block, the transfer is faster but the CPU waits longer.

Output Question · I/O Software

Question 8: No Buffering, Single and Double Buffering

A block arrives from a device in T = 8 ms, the process takes C = 5 ms to process a block, and copying from the buffer to user space takes M = 1 ms. Find the time per block in steady state and the approximate total time for 100 blocks:

  1. with no buffering,
  2. with a single buffer,
  3. with double buffering.

Does adding more buffers improve the result?

Show answer

(a) No buffering: reading and processing happen one after the other → T + C = 13 ms → about 1300 ms.

(b) Single buffer: the next block arrives in the kernel buffer while the previous one is processed, but the buffer is busy during the copy → max(T, C) + M = 8 + 1 = 9 ms → about 900 ms.

(c) Double buffer: one fills while the other is copied, and the copy overlaps too (C + M = 6 ≤ T) → max(T, C) = 8 ms → about 800 ms.

Now the device is the bottleneck (T > C): more buffers do not raise the average rate, they only absorb bursts.

Step-by-Step Trace · I/O Software

Question 9: The Circular Buffer

The interrupt handler writes with put, the process reads with get. How do head, tail, and the buffer change? Which call fails?

0·1·2·3·4·5·headtail0 / 5 full

N = 6, head = tail: empty. One slot always stays empty (at most 5 bytes). Calls: put a, b, c, d; get three times; put e, f, g, h, i.

0a1·2·3·4·5·headtail1 / 5 full

put('a'): written to slot 0, tail becomes 1.

0a1b2·3·4·5·headtail2 / 5 full

put('b'): written to slot 1, tail becomes 2.

0a1b2c3·4·5·headtail3 / 5 full

put('c'): written to slot 2, tail becomes 3.

0a1b2c3d4·5·headtail4 / 5 full

put('d'): written to slot 3, tail becomes 4.

0a1b2c3d4·5·headtail3 / 5 full

get(): head = 0 ≠ tail → a is read from slot 0, head becomes 1. The slot can be reused.

0a1b2c3d4·5·headtail2 / 5 full

get(): head = 1 ≠ tail → b is read from slot 1, head becomes 2. The slot can be reused.

0a1b2c3d4·5·headtail1 / 5 full

get(): head = 2 ≠ tail → c is read from slot 2, head becomes 3. The slot can be reused.

0a1b2c3d4e5·headtail2 / 5 full

put('e'): written to slot 4, tail becomes 5.

0a1b2c3d4e5fheadtail3 / 5 full

put('f'): written to slot 5, tail becomes 0. At the end it wraps to the start (mod 6).

0g1b2c3d4e5fheadtail4 / 5 full

put('g'): written to slot 0, tail becomes 1. A slot that was read earlier is being reused.

0g1h2c3d4e5fheadtail5 / 5 full

put('h'): written to slot 1, tail becomes 2. A slot that was read earlier is being reused.

0g1h2c3d4e5fheadtail5 / 5 full · FULL

put('i'): (tail + 1) % 6 = 3 = head → buffer full, returns −1. The producer must wait or drop the data. Consumed order: a, b, c.

int put(char c) {
  if ((tail + 1) % N == head) return -1; // full
  buf[tail] = c;
  tail = (tail + 1) % N; return 0;
}
int get(char *c) {
  if (head == tail) return -1;           // empty
  *c = buf[head];
  head = (head + 1) % N; return 0;
}

Multiple Choice · I/O Interfaces

Question 10: Return at Once, Notify When Done

A server program wants to hand a read request to the kernel and return at once, then receive a completion record when the operation finishes (like io_uring). Which I/O model is this?

C: In the asynchronous (completion) model, the call starts the work and returns; when the work finishes, the kernel adds a record to a completion queue (io_uring CQE, Windows IOCP). In the non-blocking model, the call returns only “whatever is there now” and the work remains with the process; epoll only reports readiness. The price is more complex programming: buffer lifetimes and error paths must be tracked separately.

Output Question · Clocks and Other Devices

Question 11: Pending Alarms and the Clock Counter

Clock software keeps pending alarms in a list where each entry holds its difference from the previous one. The time now is 5000 ticks; the alarms are at 5003, 5010, 5010, 5018, and 5025.

  1. Which numbers does the list hold?
  2. After 10 ticks, which alarms have fired and what is the list?
  3. The 8254 PIT counts at 1,193,182 Hz. What counter is loaded for a 250 Hz tick, and what is the actual frequency?
Show answer

(a) Differences: 5003 − 5000 = 3, 5010 − 5003 = 7, 5010 − 5010 = 0, 5018 − 5010 = 8, 5025 − 5018 = 7 → 3, 7, 0, 8, 7.

(b) At each tick only the first number is decremented: after 3 ticks the first alarm (5003) fires; after 7 more, the two alarms at 5010 fire together (the second one's difference is 0). The remaining list is 8, 7 (5018 and 5025). Work per tick is constant, independent of the number of alarms.

(c) 1,193,182 / 250 = 4772.7 → counter 4773; actual frequency 1,193,182 / 4773 ≈ 249.99 Hz. On Linux, HZ = 250 means 4 ms per tick; a tickless kernel stops these interrupts when idle.

Output Question · Disks

Question 12: How Long Does a Disk Request Take?

An HDD spinning at 10,000 rpm has an average seek time of 4 ms and a media rate of 250 MB/s (1 MB = 10⁶ B).

  1. How long does it take on average to read 64 KiB (65,536 B) of consecutive data from a random location?
  2. What if the same amount of data is read from sixteen 4 KiB blocks scattered over the disk?
  3. How much does raising the transfer rate from 250 to 500 MB/s speed up (b)?
Show answer

Access time = seek + rotational latency + transfer. One revolution is 60,000 / 10,000 = 6 ms → average rotational latency is half a revolution = 3 ms.

(a) Transfer 65,536 / 250 × 10⁶ s = 0.262 ms → 4 + 3 + 0.262 = 7.26 ms.

(b) Per block 4 + 3 + 0.016 = 7.016 ms → 16 × 7.016 = 112.3 ms: about 15 times slower.

(c) The transfer share drops from 16 × 0.016 = 0.26 ms to 0.13 ms: only 0.1% of the total. In small random reads, seek and rotation dominate the time; that is why layout, caching, and arm scheduling matter.

Step-by-Step Trace · Disk Scheduling

Question 13: Head Movement with SSTF

The head is at cylinder 84. In what order does SSTF serve the requests, and how many cylinders does the head move in total?

050100150199

Cylinders 0–199, head at 84. Pending requests (in arrival order): 184, 142, 170, 8, 60, 155, 116, 37. Red: pending, green: served.

050100150199

84 → 60: 24 cylinders, total 24. The nearest is 60 (24 away), while 116 is 32 away.

050100150199

60 → 37: 23 cylinders, total 47.

050100150199

37 → 8: 29 cylinders, total 76.

050100150199

8 → 116: 108 cylinders, total 184. The low region is done; the head goes to the nearest remaining request, the distant 116.

050100150199

116 → 142: 26 cylinders, total 210.

050100150199

142 → 155: 13 cylinders, total 223.

050100150199

155 → 170: 15 cylinders, total 238.

050100150199

170 → 184: 14 cylinders, total 252. Total 252 cylinders (FCFS 597).

pos = 84; total = 0;
while (pending > 0) {
  r = nearest(pos);        // closest request
  total += abs(r - pos);
  pos = r;
  serve(r);
}

Output Question · Disk Scheduling

Question 14: Sweep Algorithms

Same queue: the head is at 84 and, because it last served 70, is moving up (toward higher numbers). The requests are 184, 142, 170, 8, 60, 155, 116, 37; cylinders 0–199. For FCFS, SCAN, C-SCAN, LOOK, and C-LOOK, find the path the head follows and the total movement (count the return trip in C-SCAN and C-LOOK). Rank them together with SSTF's 252.

Show answer
algorithmhead pathtotal
FCFS184 → 142 → 170 → 8 → 60 → 155 → 116 → 37597
SCAN116 → 142 → 155 → 170 → 184 → 199 → 60 → 37 → 8306
C-SCAN116 → 142 → 155 → 170 → 184 → 199 → 0 → 8 → 37 → 60374
LOOK116 → 142 → 155 → 170 → 184 → 60 → 37 → 8276
C-LOOK116 → 142 → 155 → 170 → 184 → 8 → 37 → 60328

SCAN: (199 − 84) + (199 − 8) = 306. C-SCAN: 115 + 199 + 60 = 374 (175 if the return is not counted). LOOK: 100 + 176 = 276. C-LOOK: 100 + 176 + 52 = 328.

Ranking: SSTF 252 < LOOK 276 < SCAN 306 < C-LOOK 328 < C-SCAN 374 < FCFS 597. LOOK differs from SCAN only in not going all the way to the edge (199) for nothing; C-SCAN and C-LOOK travel farther but spread waiting times more evenly.

Multiple Choice · Disk Scheduling

Question 15: The Request That Has Waited for Minutes

On a heavily loaded disk, requests keep arriving between cylinders 90 and 110, while a request at cylinder 190 has been waiting for minutes. Which algorithm is in use, and what is the problem called?

A: SSTF picks the request nearest the head at every step: it reduces total movement but is not fair; if requests keep arriving in the middle region, the one at the edge never gets its turn. The SCAN, C-SCAN, and LOOK family carries the head in one direction up to the last request, so every request is served within one sweep. FCFS is starvation-free.

Multiple Choice · SSD and NVMe

Question 16: Why “none” on NVMe?

On Linux, the default I/O scheduler for NVMe SSDs is none (it does not reorder requests). What is the reason?

D: Arm scheduling exists to reduce seek time. On an SSD, access time is almost independent of position; NVMe's multiple hardware queues (blk-mq) give each core its own queue. If fairness or a latency target is needed, mq-deadline, bfq, or kyber can be chosen; on SATA HDDs mq-deadline is a common default.

Step-by-Step Trace · RAID and Stable Storage

Question 17: XOR Parity

How is the parity block computed, how is D2 recovered when its disk fails, and how is the new parity found when D1 is updated?

D010110110D101011100D211100001D300111010P????????

A RAID 5 stripe with four data disks (8-bit sample blocks). The parity block P has not been computed yet.

D010110110D101011100D211100001D300111010P11101010

Bit-by-bit XOR: 1 if the bits differ, 0 if they are the same. D0 ⊕ D1 = 11101010.

D010110110D101011100D211100001D300111010P00001011

⊕ D2 = 00001011.

D010110110D101011100D211100001D300111010P00110001

⊕ D3 = 00110001. In every bit position the number of 1s is now even.

D010110110D101011100D2????????D300111010P00110001

D2's disk failed. Because it is known which disk was lost, a single parity is enough.

D010110110D101011100D211100001D300111010P00110001

Since x ⊕ x = 0, the lost block is the XOR of the rest: D0 ⊕ D1 ⊕ D3 ⊕ P = 11100001, identical to the original D2.

D010110110D101011100D211100001D300111010P00111000

D1 is to become Y = 01010101. Without reading the whole stripe: changed bits D1 ⊕ Y = 00001001; P ⊕ this = 00111000.

D010110110D101010101D211100001D300111010P00111000

D1 and P are written. Small write = 2 reads (old D1, old P) + 2 writes: the 4-I/O cost of RAID 5.

P = D0 ^ D1;
P = P ^ D2;
P = P ^ D3;              // parity
// D2's disk failed
D2 = D0 ^ D1 ^ D3 ^ P;
// small write: D1 → Y
P = P ^ D1 ^ Y;
D1 = Y;

Multiple Choice · RAID and Stable Storage

Question 18: RAID 6 with Six Disks

You have six 4 TB disks and set up a RAID 6 array. What is the usable capacity, and how many simultaneous disk failures can it surely tolerate?

B: RAID 6 keeps two parity blocks per stripe (P: XOR, Q: Reed–Solomon): capacity is (n − 2) × 4 = 16 TB, and it survives the loss of two disks. 20 TB / 1 disk is RAID 5; 12 TB is RAID 10 and guarantees only 1 failure (up to 3 disks in the lucky case of different pairs). Because rebuilds of large disks take hours, the risk of a second failure makes RAID 6 attractive.

Output Question · RAID and Stable Storage

Question 19: Small Random Writes

A single disk delivers 120 random IOPS. What is the small random write rate (IOPS) of a 6-disk array?

  1. RAID 0
  2. RAID 10 (3 mirrored pairs)
  3. RAID 4
  4. RAID 5
  5. RAID 6

If the disk count rises to 12, which one gets no faster at all?

Show answer
layoutI/Os per writeIOPS
RAID 01N·R = 720
RAID 102 (two copies)N·R / 2 = 360
RAID 44, two always on the parity diskR / 2 = 60
RAID 54 (2 reads + 2 writes), spread over the disksN·R / 4 = 180
RAID 66 (3 reads + 3 writes)N·R / 6 = 120

In RAID 4, every small write needs one read and one write on the single parity disk: the array drops to half the speed of that disk, and adding disks does not help at all (still 60 with 12 disks). RAID 5 spreads the same 4 I/Os over all disks, so it grows with the disk count (360 with 12 disks).

Classic Question · RAID and Stable Storage

Question 20: A Stable Write Interrupted Midway

In stable storage, a block is first written to disk 1 and verified by reading it back, then the same is done on disk 2. During recovery the two copies are compared; a bad block is detected by ECC.

What does recovery do if the CPU crashes (a) while writing to disk 1, (b) after disk 1 is written but before disk 2 is started, (c) while writing to disk 2? Why is the result in every case either entirely old or entirely new? Why does RAID 1 not guarantee this by itself?

Show answer
  • (a) The block on disk 1 is half written and gives an ECC error; the old value is copied from disk 2 to disk 1 → old.
  • (b) Both copies are valid but different; rule: disk 1 wins, and the new value is written to disk 2 → new.
  • (c) Disk 2 is bad (ECC), disk 1 is new and valid; the new value is copied to disk 2 → new.

Because the write order is fixed, at any moment at least one copy is either entirely old or entirely new and valid; recovery spreads that copy to the other. RAID 1 writes to both disks at once and keeps no order saying which one is “correct”: it protects against a bad sector, not against a crash during a write. Today the same idea is implemented with journaling, write barriers/FUA, and SSDs with power-loss protection.

The End

Chapter 11: Input/Output: Study Questions
Lecture slides: Chapter 11: Input/Output · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.