Operating Systems Cheatsheet

I/O Systems

Use this Operating Systems reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

I/O Hardware Overview

I/O devices communicate with the CPU via ports, buses, and device controllers.

ComponentRole
Device controllerManages one device type; has registers (status, command, data)
I/O portFixed address in I/O address space; CPU uses in/out instructions
Memory-mapped I/ODevice registers mapped into physical address space; use regular load/store
BusShared communication path (PCIe, USB, SATA, NVMe)
DMA controllerTransfers data between device and memory without CPU involvement

I/O Interaction Methods

MethodCPU involvementUse case
Polling (busy wait)100% (spin-reads status register)Very fast devices, real-time
Interrupt-drivenOnly on completionGeneral purpose
DMASetup only; device writes directly to RAMHigh-bandwidth (disk, NIC)

DMA Transfer Sequence

1. CPU programs DMA: source, dest (physical addr), count, direction
2. CPU resumes other work
3. DMA controller transfers data word-by-word (or burst) over bus
4. DMA raises interrupt when done
5. CPU handles interrupt: verify status, continue

Cycle stealing: DMA steals bus cycles from CPU; slight CPU slowdown but overall faster than CPU copying.

I/O Software Layers

┌───────────────────────────────┐
│  User-level I/O library       │  printf, fread, fwrite (buffering)
├───────────────────────────────┤
│  Device-independent OS layer  │  open/read/write syscalls, naming, buffering, spooling
├───────────────────────────────┤
│  Device drivers               │  Per-device; interrupt handlers
├───────────────────────────────┤
│  Interrupt handlers           │  Save state, service, signal upper layer
├───────────────────────────────┤
│  Hardware                     │  Device controllers
└───────────────────────────────┘

Device Drivers

Each device driver implements a standard interface (struct file_operations in Linux):

Function pointerPurpose
openInitialize device
releaseCleanup
readTransfer data from device to user
writeTransfer data from user to device
ioctlDevice-specific control commands
mmapMap device memory to user space
pollSupport select/poll/epoll
llseekReposition file pointer

Disk I/O and Scheduling

Disk Structure

  • Plattertracksector (512B or 4096B)
  • Cylinder: same track number across all platters
  • Seek: move arm to track (10–20 ms)
  • Rotational latency: wait for sector (avg = half rotation ≈ 4 ms at 7200 RPM)
  • Transfer: read/write (small)

Total time = seek time + rotational latency + transfer time

Disk Scheduling Algorithms

Reference string of track requests: 98, 183, 37, 122, 14, 124, 65, 67 (head starts at 53)

AlgorithmOrderTotal seek (tracks)Notes
FCFS53→98→183→37→122→14→124→65→67640Simple, no starvation
SSTF (Shortest Seek Time First)53→65→67→37→14→98→122→124→183236Starvation of outer tracks
SCAN (Elevator)53→65→67→98→122→124→183→37→14236No starvation
C-SCAN (Circular SCAN)53→65→67→98→122→124→183→14→37382Uniform wait time
LOOKLike SCAN but only goes as far as last request~208Better than SCAN
C-LOOKCircular LOOKBetter than C-SCANDefault on many OSes

SSDs have no moving parts — seek/rotational latency irrelevant. NVMe SSDs use parallel I/O queues; disk scheduling is minimal.

I/O Buffering

StrategyDescriptionUse
No bufferingData copied directly between device and userRare (real-time)
Single bufferOS buffer; process works while OS fills nextSimple
Double bufferTwo alternating buffers; overlap I/O + processingBetter throughput
Circular bufferRing of buffers; producer/consumer modelStreaming

Spooling (Simultaneous Peripheral Operations On-Line): writes go to disk queue (spool); device drains queue. Used for printers, batch jobs.

Block vs Character Devices

TypeAccessExamplesBuffering
Block deviceRandom; fixed-size blocksDisk, SSD, USB driveYes (page cache)
Character deviceSequential streamKeyboard, serial port, terminalNo (or line buffer)

I/O Completion Notification

MechanismDescriptionAPI
Blocking I/OThread sleeps until I/O doneread() default
Non-blocking I/OReturns immediately; EAGAIN if not readyO_NONBLOCK flag
Asynchronous I/O (AIO)Submit request; get notified on completionio_submit, io_uring
select / pollMonitor multiple fds; return when any readyselect(nfds, …)
epollScalable event notification; O(1) per eventepoll_create, epoll_wait
io_uring (Linux 5.1+)Ring buffers for zero-syscall async I/Oio_uring_setup

select vs epoll Scaling

select / pollepoll
Syscall per waitO(n) — pass all fdsO(1) — kernel tracks
Max fds1024 (FD_SETSIZE)Millions
New event scanO(n) kernel scanO(1) event list
Suitable forSmall nHigh-connection servers

RAID Levels

Redundant Array of Independent Disks — combine multiple disks for performance and/or reliability.

LevelMin disksRedundancyRead perfWrite perfUsable space
RAID 0 (striping)2None100%
RAID 1 (mirroring)2Full copy2× read50%
RAID 43Parity diskN−1×1× (parity bottleneck)(N−1)/N
RAID 53Distributed parity~N× (parallel writes)(N−1)/N
RAID 64Dual paritySlower writes(N−2)/N
RAID 10 (1+0)4Mirrored stripesN/2×50%

RAID is not a backup — protects against disk failure, not accidental deletion or corruption.

Kernel I/O Subsystem: Page Cache

Disk reads cached in memory (page cache). Subsequent reads served from RAM. Writes: write-back (delay to disk) or write-through (immediate).

dirty ratio: max % of RAM that can be dirty pages before writes are forced (/proc/sys/vm/dirty_ratio).