These are the notes for CS Fundamentals for Placements, episode 38, the last operating-systems episode. In episode 37 we scheduled a disk's arm. Today: how does the CPU actually talk to a disk, a keyboard or a printer, and how can one machine pretend to be many? We invent the I/O hardware pieces (device, port, bus, device controller, device driver), isolated (port-mapped) vs memory-mapped I/O, polling and programmed I/O, interrupt-driven I/O, DMA (with burst mode and cycle stealing), blocking, non-blocking and asynchronous I/O, buffering (single, double, circular), caching, spooling, character vs block devices, the kernel I/O subsystem, and then virtualization: virtual machines, type 1 and type 2 hypervisors, containers, namespaces and cgroups. Every term is explained in plain words the first time it appears, and there is a full glossary at the end.
All program outputs below are real: every C program was compiled with gcc 13.3 -O2 (the SyntaxPilot compiler's flags: gcc -O2 -Wno-unused-result main.c -o main -lm) and run on Linux x86-64, as root, on 6 November 2026. Two programs print timings and counts that change a little from run to run; that is said next to them.
Quick recap
- The CPU runs instructions; main memory (RAM) holds running programs; the bus (episode 6) is the set of shared wires connecting them.
- An interrupt (episode 6, 15) is a signal that makes the CPU pause, run an interrupt service routine (ISR), and resume.
- A system call (episode 15) is how a program asks the kernel to do something, like
readorwrite.
1. I/O hardware
I/O (input/output) means any data moving between the computer and a device: a keystroke coming in, a page going to a printer, a block to or from a disk.
The story: ordering at a restaurant
| Restaurant | Computer | Meaning |
|---|---|---|
| The kitchen | Device | The hardware doing the real work: disk, keyboard, printer, network card. |
| The order counter (slip in, "ready" light, plate out) | Device controller | A small chip that operates the device. It has a few registers the CPU can read and write: command, status, data-in, data-out. |
| A waiter who knows this kitchen | Device driver | Kernel code that knows how to drive one kind of controller. The rest of the OS says "read block 500"; the driver turns it into register writes. |
- Bus: shared wires connecting the CPU, memory and controllers (e.g. PCIe).
- Port: a numbered connection point through which a device's registers are reached.
How the CPU reaches a controller's registers
| Isolated I/O (port-mapped) | Memory-mapped I/O (MMIO) | |
|---|---|---|
| Addresses | a separate I/O address space | registers get ordinary memory addresses |
| Instructions | special ones (in, out on x86) |
normal load/store (mov) |
| Cost | none of the memory address space | uses part of the address space |
| Used by | older/legacy PC devices | most modern devices (e.g. graphics cards) |
2. Waiting for a device: polling vs interrupts
The story: waiting for your chai
You order chai at a stall. You can ask "ready?" every two seconds (and do nothing else), or the chaiwala can call your name while you sit and read.
- Polling (busy waiting): the CPU reads the controller's status register in a loop until the "busy" bit clears.
- Programmed I/O (PIO): the CPU itself does every step of the transfer, moving each byte or word between the controller and memory (usually with polling).
- Interrupt-driven I/O: the CPU starts the operation and goes on with other work; when the device is done it raises an interrupt, and the CPU runs the device's ISR.
Live: polling burns the CPU, blocking doesn't
A child process plays the "device": it writes one byte into a pipe (episode 19) after one second, and another byte a second later.
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
// A "device" (child process) delivers one byte after 1 second
int main(void) {
int p[2]; char c; pipe(p);
if (fork() == 0) { // the device
sleep(1); write(p[1], "A", 1); sleep(1); write(p[1], "B", 1); return 0;
}
fcntl(p[0], F_SETFL, O_NONBLOCK); // 1) polling: keep asking
long checks = 0; clock_t t = clock();
while (read(p[0], &c, 1) != 1) checks++; // "is it ready yet?"
printf("polling : got %c after %ld checks, CPU used %.2f s\n",
c, checks, (double)(clock() - t) / CLOCKS_PER_SEC);
fcntl(p[0], F_SETFL, 0); // 2) blocking: sleep until ready
t = clock();
read(p[0], &c, 1); // the OS wakes us up
printf("blocking: got %c, CPU used %.2f s\n",
c, (double)(clock() - t) / CLOCKS_PER_SEC);
}
Output (the number of checks changes a little every run; it is always in the millions):
polling : got A after 6438521 checks, CPU used 1.00 s
blocking: got B, CPU used 0.00 s
clock() measures CPU time used by the process, not wall-clock time. Polling burnt a whole second of CPU just asking. The blocking read waited the same second but used no CPU: the kernel put the process to sleep and woke it when data arrived.
Is polling ever right?
Every interrupt has a cost: save registers, run the ISR, restore. If the device finishes in a few CPU cycles, one or two polls are cheaper than an interrupt. Poll for extremely fast devices; use interrupts for long or unpredictable waits. Busy network drivers switch between the two under heavy load (Linux's NAPI does this).
3. DMA: moving whole blocks without the CPU
Live: an interrupt per byte can't keep up
#include <stdio.h>
// A device sends 1 MB per second. Every interrupt costs the CPU 2 microseconds
int main(void) {
double rate = 1e6, cost = 2e-6; // bytes/s, seconds per interrupt
double perByte = rate * cost; // 1 interrupt per byte
printf("interrupt per byte : %.0f interrupts/s -> CPU busy %.0f%%\n",
rate, perByte * 100);
double block = 4096; // DMA: 1 interrupt per 4 KB block
double dma = rate / block * cost;
printf("DMA, 4 KB blocks : %.0f interrupts/s -> CPU busy %.3f%%\n",
rate / block, dma * 100);
}
Output:
interrupt per byte : 1000000 interrupts/s -> CPU busy 200%
DMA, 4 KB blocks : 244 interrupts/s -> CPU busy 0.049%
200% is impossible: the CPU can't keep up at all.
The story: the courier with a building key
Without a key, you carry each parcel up yourself (the CPU copies every byte). Give the courier a key ("put all 40 boxes in flat 12") and you're free; one doorbell when it's done.
DMA (direct memory access): the CPU programs a DMA controller with the device, the memory address, the byte count and the direction; the DMA controller moves the data between the device and RAM by itself, and raises one interrupt when the whole block is done.
DMA modes: sharing the bus
The DMA controller and the CPU share the memory bus, so DMA takes bus cycles away from the CPU.
| Mode | How | Effect on the CPU |
|---|---|---|
| Burst (block) mode | DMA takes the bus for the whole block | CPU waits for memory until the block is done; fastest transfer |
| Cycle stealing | DMA takes the bus for one word, gives it back, repeats | CPU slows a little, never stops for long |
| Transparent mode | DMA uses the bus only when the CPU isn't using it | no slowdown; slowest transfer |
DMA numericals
Burst mode: if preparing the transfer takes X and transferring the block takes Y, the CPU is blocked for Y out of every X + Y:
% CPU blocked (burst mode) = Y / (X + Y) × 100
Example (GATE style): X = 2 µs to prepare, Y = 18 µs to transfer → 18 / 20 = 90% blocked.
Interrupt overhead: % CPU = (interrupts per second) × (time per interrupt). The video's quiz: 50,000 bytes/s, one interrupt per byte, 4 µs each → 50,000 × 4 µs = 0.2 s per second = 20%.
4. Blocking, non-blocking and asynchronous I/O
| Style | What read does |
Analogy | In the demo |
|---|---|---|---|
| Blocking | waits until data is there; the process sleeps | standing at the counter | the second read |
| Non-blocking | returns at once with what's ready, or an error (EAGAIN on Linux) meaning "try again" |
asking "ready?" and walking off | the polling loop (O_NONBLOCK) |
| Asynchronous | starts the I/O and returns; you are notified when the whole operation completes | food-court buzzer | (e.g. Linux io_uring, POSIX aio_read) |
Non-blocking tells you "nothing yet"; asynchronous does the work and tells you "done". Servers handling thousands of connections rely on non-blocking I/O with readiness notification (select, poll, epoll) or on async I/O.
5. Buffering, caching and spooling
A buffer is memory that holds data on its way from one place to another.
Live: one system call per byte vs a buffer
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
double now(void) {
struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec + t.tv_nsec / 1e9;
}
int main(void) {
int n = 200000; // write 200,000 bytes
int fd = open("a.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
double t = now();
for (int i = 0; i < n; i++) write(fd, "x", 1); // 1 system call per byte
printf("no buffer : %6d system calls, %.3f s\n", n, now() - t);
close(fd);
FILE *f = fopen("b.txt", "w"); // stdio keeps a 4 KB buffer
t = now();
for (int i = 0; i < n; i++) fputc('x', f); // fills the buffer in RAM
fclose(f); // a write() per full buffer
printf("with buffer: %6d system calls, %.3f s\n", (n + 4095) / 4096,
now() - t);
}
Output (times vary a little from run to run):
no buffer : 200000 system calls, 0.058 s
with buffer: 49 system calls, 0.001 s
The second count is computed (200,000 / 4096, rounded up); it was checked with strace -c, a tool that counts a program's system calls: 200,050 write calls in total = 200,000 + 49 + 1 (the program's own output). glibc's stdio chose a 4096-byte buffer for this file.
Why buffer
- Speed mismatch: a fast producer and a slow consumer, or the reverse (a network card and a disk).
- Size mismatch: small packets arrive, the file system wants whole blocks.
- Copy semantics: once
writereturns, the program may reuse its array; the kernel already copied the data into its own buffer.
Three kinds of buffering
| Kind | How | Weakness / strength |
|---|---|---|
| Single buffer | the device fills it, then the program empties it, in turns | one side always waits |
| Double buffer | the device fills buffer A while the program processes B, then they swap | both work at once; games draw frames this way |
| Circular buffer (ring buffer) | n slots in a ring; producer writes at the tail, consumer reads at the head; indexes wrap with % n |
continuous streams: keyboards, network cards, audio |
Live: a circular buffer
#include <stdio.h>
// Circular buffer: 4 slots, the indexes wrap around with % SIZE
#define SIZE 4
int buf[SIZE], head = 0, tail = 0, count = 0;
int put(int x) { // the device adds at tail
if (count == SIZE) return -1; // full: device must wait
int slot = tail; buf[tail] = x; tail = (tail + 1) % SIZE; count++;
return slot;
}
int get(void) { int x = buf[head]; head = (head + 1) % SIZE; count--; return x; }
int main(void) {
for (int i = 1; i <= 5; i++) printf("put %d -> slot %d\n", i, put(i));
for (int k = 0; k < 2; k++) printf("get %d\n", get()); // the program reads 2
printf("put 6 -> slot %d (wrapped round to the start)\n", put(6));
while (count > 0) printf("get %d\n", get());
}
Output:
put 1 -> slot 0
put 2 -> slot 1
put 3 -> slot 2
put 4 -> slot 3
put 5 -> slot -1
get 1
get 2
put 6 -> slot 0 (wrapped round to the start)
get 3
get 4
get 6
Slot −1 means "full": item 5 was refused (a real device would wait or drop it). This is the bounded buffer of the producer-consumer problem (episode 27), without the locking.
Caching vs buffering
- A cache is a faster copy of data that also lives somewhere else (the page cache of episode 36). Lose it, lose nothing.
- A buffer may hold the only copy while the data is in transit. Lose it, lose data.
- The same memory can be both: the page cache buffers writes and caches reads.
Spooling
Ten people print at once, but a printer prints one job at a time and can't interleave pages. Spooling saves each whole job to a queue on disk (the spool), and the printer takes them one after another. SPOOL stands for Simultaneous Peripheral Operations On-Line. Your OS's print queue is a spool.
6. Kinds of devices and the kernel I/O subsystem
| Character device | Block device | |
|---|---|---|
| Data | a stream of bytes, in order | fixed-size blocks, any block by number |
| Seek / random access | usually no | yes |
| Examples | keyboard, mouse, serial port, /dev/null |
disk, SSD, pen drive |
Linux ls -l /dev |
first letter c |
first letter b |
(Network devices are a third kind, used through sockets.)
The kernel I/O subsystem is the part of the kernel that provides: I/O scheduling (episode 37), buffering, caching, spooling, device reservation (giving a device to one user at a time), error handling (retry a failed read, report errors), and protection (all I/O instructions are privileged, episode 15, so programs must go through system calls).
The path of a read: program → system call → kernel I/O subsystem (cache? if hit, return) → device driver → controller → device → interrupt → driver → data copied to the program → program woken.
7. Virtualization
The story: one building, many flats
| Building | Virtualization | Meaning |
|---|---|---|
| One building on one plot | Host hardware | the one real machine |
| Separate flats, each with its own kitchen | Virtual machines (VMs) | software computers, each running its own guest OS |
| The building manager | Hypervisor (virtual machine monitor, VMM) | shares the real CPU, memory and disk among VMs and keeps them isolated |
Virtualization means making one physical machine appear as several independent machines.
Type 1 and type 2 hypervisors
| Type 1 (bare metal) | Type 2 (hosted) | |
|---|---|---|
| Runs on | directly on the hardware | as an application on a normal OS |
| Speed | faster (no host OS in the way) | slower |
| Used in | data centres, clouds | laptops, testing |
| Examples (AWS) | VMware ESXi, Microsoft Hyper-V, KVM | Oracle VM VirtualBox, VMware Workstation |
Source: AWS, "What's the difference between Type 1 and Type 2 hypervisors?": a type 1 hypervisor "sits on top of the bare metal server and has direct access to the hardware resources"; a type 2 hypervisor "is an application installed on the host operating system".
Modern CPUs have hardware support for virtualization (Intel VT-x, AMD-V) so guest kernels can run at full speed while the hypervisor stays in control.
8. Containers, namespaces and cgroups
The story: a PG hostel
A VM is heavy: each one boots a full OS with its own kernel. A PG (paying guest) hostel has one shared kitchen, but each room looks private and has its own electricity meter.
| PG hostel | Containers |
|---|---|
| One shared kitchen | all containers share the host's kernel |
| Each room looks private (lock, name plate) | namespaces: each container gets its own view of hostname, PIDs, mounts, network... |
| Each room's power meter and limit | cgroups: limits on CPU, memory, I/O |
A container is an isolated group of processes that shares the host's kernel but has its own namespaces and resource limits, plus its own file-system image.
Namespaces (from the namespaces(7) manual page): each type isolates one thing.
| Namespace | Flag | Isolates |
|---|---|---|
| Cgroup | CLONE_NEWCGROUP |
cgroup root directory |
| IPC | CLONE_NEWIPC |
System V IPC, POSIX message queues |
| Network | CLONE_NEWNET |
network devices, stacks, ports |
| Mount | CLONE_NEWNS |
mount points |
| PID | CLONE_NEWPID |
process IDs |
| Time | CLONE_NEWTIME |
boot and monotonic clocks |
| User | CLONE_NEWUSER |
user and group IDs |
| UTS | CLONE_NEWUTS |
hostname and NIS domain name |
Live: a tiny container
#define _GNU_SOURCE
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
#include <sys/wait.h>
// A container is just a process with its OWN namespaces
int main(void) {
char h[64]; gethostname(h, sizeof h);
printf("host : hostname=%s, pid=%d\n", h, getpid()); fflush(stdout);
if (fork() == 0) {
unshare(CLONE_NEWUTS | CLONE_NEWPID); // new hostname + PID spaces
if (fork() == 0) { // first process inside
sethostname("box1", 4); gethostname(h, sizeof h);
printf("container : hostname=%s, pid=%d\n", h, getpid());
return 0;
}
wait(NULL); return 0;
}
wait(NULL); gethostname(h, sizeof h);
printf("host again: hostname=%s (unchanged)\n", h);
}
Output (the host's name and PID depend on the machine; this ran on one called vm):
host : hostname=vm, pid=1919
container : hostname=box1, pid=1
host again: hostname=vm (unchanged)
unsharemoves the calling process into new namespaces. A new PID namespace applies to the caller's children, so the first child inside is PID 1, likeinit.fflush(stdout)beforeforkmatters: when output goes to a pipe,printftext waits in a buffer, andforkwould copy the unprinted buffer into the children, printing the first line three times. (Buffering from section 5, biting back.)- It needs root (the
CAP_SYS_ADMINcapability).
cgroups
From the kernel's Control Group v2 documentation: "cgroup is a mechanism to organize processes hierarchically and distribute system resources". Controllers include cpu, memory, io and pids. For memory: "If a cgroup's memory usage reaches this limit [memory.max] and can't be reduced, the OOM killer is invoked in the cgroup" (the OOM killer is the kernel code that kills a process when memory runs out); only that group suffers, not the whole machine.
Container tools such as Docker build containers from these kernel features (namespaces + cgroups) plus an image of the application's files (source: Microsoft Q&A, "How is Docker different from a virtual machine?").
Containers vs virtual machines
| Virtual machine | Container | |
|---|---|---|
| Kernel | its own guest kernel | shares the host's |
| Isolation | hardware-level, strong | namespaces + cgroups, weaker |
| Start-up | seconds to minutes (boots an OS) | about a second or less |
| Size | gigabytes | megabytes |
| Can run | any OS (Windows on a Linux host) | programs for the host's kernel only |
| Managed by | hypervisor | container runtime |
In clouds you often get both: containers running inside VMs.
The whole chain
| Problem | Invention |
|---|---|
| CPU must operate many different devices | controller registers + device driver |
| How to reach the registers | port-mapped or memory-mapped I/O |
| Asking again and again wastes the CPU | interrupts (and blocking I/O) |
| One interrupt per byte is too many | DMA: one interrupt per block |
| DMA and CPU fight for the bus | burst, cycle stealing, transparent modes |
| Tiny transfers, speed and size mismatch | buffering: single, double, circular |
| A device that can do one job at a time | spooling |
| Many OSes on one machine | VMs and hypervisors |
| VMs are heavy | containers: namespaces + cgroups |
Common mistakes
| Mistake | What you see | Why | Fix |
|---|---|---|---|
| "DMA never affects the CPU" | wrong theory answer | they share the bus | burst stalls it, cycle stealing slows it |
| Calling a buffer a cache | lost marks | cache = extra copy, buffer = maybe the only copy | name them correctly |
| "Non-blocking = asynchronous" | wrong interview answer | non-blocking returns "not ready"; async completes and notifies | compare with EAGAIN vs completion |
| "A container has its own kernel" | wrong interview answer | it shares the host's | VM = own kernel |
| Busy-polling slow events | 100% CPU, hot laptop | busy waiting | block, or use interrupts / async I/O |
fork() with unflushed printf |
lines printed several times when output is piped | the stdio buffer is copied into the child | fflush(stdout) before fork |
unshare as a normal user |
it returns -1 with errno EPERM ("Operation not permitted") and nothing is isolated | needs CAP_SYS_ADMIN (or a user namespace) |
run as root, or add CLONE_NEWUSER |
| Mixing 1 MB = 10⁶ and 2²⁰ | answers slightly off | units differ | say which one you use |
Mini project: a mini spooler
Write a C program with a circular buffer of 8 "print jobs" (job id + number of pages). One thread (the users) adds jobs at random moments; another thread (the printer) takes one job at a time and "prints" it (sleeps for pages × 100 ms). Protect the buffer with a mutex and two condition variables (episode 26) so that a full spool makes users wait and an empty spool makes the printer sleep. Print a log like job 3 queued (slot 2), printing job 3 (4 pages). Bonus: give jobs a priority and print the highest first.
Practice
1. A device sends 2 MB/s; it interrupts once per 512 bytes, and each interrupt costs 10 µs. What fraction of CPU time goes to interrupts?
Solution
With 1 MB = 10⁶ B: 2,000,000 / 512 = 3906.25 interrupts/s × 10 µs = 39,062.5 µs per second ≈ 3.9%. (With 1 MB = 2²⁰ B: 4096 interrupts/s → 40,960 µs ≈ 4.1%.)
2. Burst-mode DMA: preparing takes 2 µs and transferring the block takes 18 µs. What percentage of time is the CPU blocked?
Solution
Y / (X + Y) = 18 / (2 + 18) = 90%.
3. Add CLONE_NEWNS (a new mount namespace) to the demo. What changes?
Solution
The container gets its own copy of the mount table: anything it mounts (say a tmpfs on /mnt) is invisible to the host, and unmounting inside doesn't affect the host. With CLONE_NEWPID plus a new mount namespace you can also mount a fresh /proc, so ps inside shows only the container's processes. (On systems where / is a shared mount, also make mounts private first, e.g. mount --make-rprivate /.)
4. VM or container: (a) run a Windows-only app on a Linux server, (b) run 50 copies of the same Linux web app?
Solution
(a) VM: a container shares the Linux kernel and can't run a Windows kernel. (b) Containers: same kernel, megabytes each, start in about a second.
5. Memory-mapped or isolated I/O: the CPU writes to address 0xFEE00000 with an ordinary mov instruction and a device reacts. Which one?
Solution
Memory-mapped I/O: the device register has an ordinary memory address and is reached with normal load/store instructions.
6. Why does double buffering help a video player?
Solution
While the screen shows (or the decoder fills) one buffer, the next frame is prepared in the other; then they swap. Neither side waits for the other, and the viewer never sees a half-drawn frame.
Quick quiz
rate = 50,000 bytes/second
interrupt = one per byte
cost = 4 µs per interrupt
// % of CPU time on interrupts?
A. 20% B. 2% C. 50% D. 0.2%
Answer
A. 50,000 interrupts/s × 4 µs = 200,000 µs = 0.2 s of every second = 20%.
Interview questions
- What does a device driver do? Translates the kernel's generic requests into commands for one kind of controller, and handles its interrupts.
- Memory-mapped vs port-mapped I/O? Registers at memory addresses with normal instructions vs a separate I/O space with
in/out. - Polling vs interrupts: when is each better? Polling for very fast devices (cheaper than an interrupt); interrupts for long or unpredictable waits.
- What is DMA and why is it needed? A controller that copies whole blocks between a device and RAM without the CPU, raising one interrupt at the end; per-byte interrupts can't keep up with fast devices.
- What is cycle stealing? DMA taking the bus for one word at a time between CPU cycles.
- Blocking vs non-blocking vs asynchronous I/O? Waits; returns "not ready" (EAGAIN); starts and notifies on completion.
- Buffer vs cache vs spool? In-transit data; a faster extra copy; a queue of whole jobs for a one-at-a-time device.
- What is double buffering? Two buffers, one filled while the other is used, then swapped.
- Character vs block device? Byte stream vs randomly addressable fixed-size blocks.
- Type 1 vs type 2 hypervisor? Bare metal (ESXi, Hyper-V, KVM) vs hosted on an OS (VirtualBox, VMware Workstation).
- Container vs VM? Shared kernel with namespaces and cgroups vs its own kernel on a hypervisor.
- What are namespaces and cgroups? Namespaces give a process its own view (PID, UTS, mount, network...); cgroups limit and account for resources (CPU, memory, io, pids).
Glossary
| Term | Meaning in plain words | Example |
|---|---|---|
| I/O | Data moving between the computer and a device | a keystroke, a disk block |
| Device | Hardware that does the real work | disk, keyboard, printer |
| Bus | Shared wires connecting CPU, memory and controllers | PCIe |
| Port | A numbered point where a device's registers are reached | x86 I/O port 0x60 |
| Device controller | Chip operating a device, with registers | disk controller |
| Status / command / data registers | Controller registers: state, orders, data in/out | busy bit |
| Device driver | Kernel code for one kind of controller | NVMe driver |
| Isolated (port-mapped) I/O | Separate I/O address space, in/out instructions |
legacy PC keyboard |
| Memory-mapped I/O | Device registers at memory addresses | graphics card |
| Polling (busy waiting) | Repeatedly checking a status until ready | 6 million checks |
| Programmed I/O | CPU moves every byte itself | polling loop |
| Interrupt | Device signal that makes the CPU run an ISR | data ready |
| ISR (interrupt service routine) | Function the CPU runs for an interrupt | the disk's handler |
| Interrupt-driven I/O | Start I/O, continue, get interrupted when done | blocking read |
| DMA (direct memory access) | A controller copies blocks device ↔ RAM without the CPU | 1 interrupt per 4 KB |
| DMA controller | The chip that performs DMA | |
| Burst mode | DMA holds the bus for a whole block | CPU blocked 90% |
| Cycle stealing | DMA takes one bus cycle at a time | slight slowdown |
| Transparent mode | DMA uses the bus only when idle | no slowdown |
| Blocking I/O | The call waits; the process sleeps | read on a pipe |
| Non-blocking I/O | The call returns at once with data or EAGAIN | O_NONBLOCK |
| EAGAIN | Linux error "try again, nothing ready" | polling loop |
| Asynchronous I/O | Start now, be notified when complete | food-court buzzer |
clock() |
CPU time used by the process | 1.00 s polling |
| Buffer | Memory holding data in transit | stdio's 4 KB |
| Single / double buffering | One buffer used in turns / two swapped | video frames |
| Circular (ring) buffer | Slots in a ring, indexes wrap with % |
keyboard buffer |
| Cache | A faster extra copy of data | page cache |
| Spooling (SPOOL) | Queue whole jobs on disk for a one-at-a-time device | print queue |
strace |
Tool listing a program's system calls | strace -c |
| Character device | Byte-stream device | /dev/null, keyboard |
| Block device | Device of numbered fixed-size blocks | disk |
| Kernel I/O subsystem | Kernel layer doing scheduling, buffering, caching, spooling, errors, protection | |
| Device reservation | Giving a device to one user at a time | a tape drive |
| Virtualization | One machine appearing as several | a cloud server |
| Virtual machine (VM) | A software computer running its own OS | a Linux VM |
| Host / guest | The real machine / the OS inside a VM | Windows guest on a Linux host |
| Hypervisor (VMM) | Software that runs and isolates VMs | KVM |
| Type 1 hypervisor | Runs directly on hardware | VMware ESXi, Hyper-V, KVM |
| Type 2 hypervisor | An application on a host OS | VirtualBox, VMware Workstation |
| Container | Isolated processes sharing the host kernel | a Docker container |
| Namespace | A private view of one kernel resource | UTS, PID, mount, network |
unshare |
System call that moves a process into new namespaces | CLONE_NEWUTS |
| UTS namespace | Own hostname | box1 |
| PID namespace | Own process numbers; first process is PID 1 | pid=1 |
| cgroup (control group) | Groups processes and limits their resources | memory.max |
| OOM killer | Kernel code that kills a process when memory runs out | inside the cgroup |
| Docker | A popular container tool built on namespaces and cgroups |
Summary
- The CPU talks to a device controller's registers (command, status, data) through a device driver, using port-mapped or memory-mapped I/O.
- Polling wastes CPU (live: millions of checks, about 1 s of CPU); interrupts and blocking calls let the CPU do other work (live: 0.00 s).
- One interrupt per byte can't keep up (live: 200% CPU); DMA moves whole blocks with one interrupt (live: 0.049%), in burst, cycle stealing or transparent mode.
- Blocking, non-blocking (EAGAIN) and asynchronous I/O; buffering cuts system calls (live: 200,000 vs 49), comes as single, double and circular (live ring buffer wrapping to slot 0); caches are copies, spools queue whole jobs.
- Character vs block devices; the kernel I/O subsystem schedules, buffers, caches, spools, reserves, handles errors and protects.
- VMs run their own OS on a hypervisor (type 1 bare metal: ESXi, Hyper-V, KVM; type 2 hosted: VirtualBox, VMware Workstation). Containers share the host kernel, isolated by namespaces (live: hostname box1, PID 1) and limited by cgroups.
Next episode: DBMS introduction and architecture: why plain files fail, what a DBMS adds, data models and NoSQL, schema vs instance, the three-schema architecture, data independence and SQL's command families.