Operating Systems Cheatsheet

Processes

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.

Process Fundamentals

A process is a program in execution — the unit of work in an OS. A program is passive (a file on disk); a process is active (has CPU state, memory, resources).

Each process has: an address space, CPU registers (saved when not running), open file descriptors, a priority, and accounting information — all stored in the Process Control Block (PCB).

Process Memory Layout

High address
┌─────────────────┐
│    Stack        │  ← grows downward (local vars, return addrs)
│       ↓         │
│   (gap)         │
│       ↑         │
│    Heap         │  ← grows upward (malloc/new)
├─────────────────┤
│  BSS segment    │  uninitialized global/static data (zeroed)
├─────────────────┤
│  Data segment   │  initialized global/static data
├─────────────────┤
│  Text segment   │  read-only code + constants
└─────────────────┘
Low address

Process Control Block (PCB) Fields

FieldContents
PIDUnique process identifier
StateRunning, Ready, Waiting, …
Program CounterAddress of next instruction
CPU RegistersAll general-purpose + status registers
Memory InfoBase/limit registers or page table pointer
I/O StatusList of open files, pending I/O
AccountingCPU time used, priority, scheduling data
Parent PIDPPID of creator

Process States

        admit
New ──────────→ Ready ←──────── Waiting
                  │    I/O done   ↑
           dispatch│              │ I/O request
                  ↓              │
               Running ──────────┘
                  │
            exit / abort
                  ↓
              Terminated
StateMeaning
NewBeing created
ReadyIn memory, waiting for CPU
RunningInstructions executing on CPU
Waiting (Blocked)Waiting for event (I/O, signal)
TerminatedFinished; PCB not yet reaped

Process Creation

pid_t pid = fork();     // clone current process
if (pid == 0) {
    execve("/bin/ls", argv, envp);  // replace image
} else {
    wait(NULL);         // parent waits for child
}
  • fork() duplicates parent → child gets a copy-on-write clone of address space
  • exec*() replaces the process image (code, data, stack) — PID stays the same
  • On Windows: CreateProcess() combines fork + exec in one call

fork() Return Values

ContextReturn value
Parent processChild's PID (> 0)
Child process0
Error−1

Process Termination

CauseMechanism
Normal exitexit(status) / return from main
Error exitexit(1), uncaught exception
Fatal signalSIGSEGV, SIGKILL, …
Killed by parentkill(pid, SIGTERM)

Zombie: child terminated, parent hasn't called wait() — PCB still in table. Orphan: parent terminated before child — child re-parented to PID 1 (init/systemd).

Inter-Process Communication (IPC)

MechanismKernel involvementDirectionUse case
Pipe (anonymous)YesUnidirectionalParent ↔ child
Named pipe (FIFO)YesUnidirectionalUnrelated processes
Message queueYesBothStructured messages
Shared memorySetup onlyBothHighest throughput
SocketYesBothNetwork or local
SignalYesOne-way notificationAsync events
mmap (file-backed)Setup onlyBothLarge data sharing

Pipe Example

int fd[2];
pipe(fd);          // fd[0]=read end, fd[1]=write end
if (fork() == 0) {
    close(fd[1]);
    read(fd[0], buf, sizeof(buf));
} else {
    close(fd[0]);
    write(fd[1], "hello", 5);
}

Context Switch

When the OS switches the CPU from process A to process B:

  1. Save A's CPU registers into A's PCB
  2. Update A's state (Ready or Waiting)
  3. Load B's PCB registers into CPU
  4. Update B's state to Running
  5. Jump to B's saved PC

Cost: pure overhead — no useful work done. Typical cost: 1–10 µs (cache flush, TLB flush, register load).

Process Scheduling Queues

QueueContents
Job queueAll processes in the system
Ready queueProcesses in memory, ready to run
Device queuesProcesses waiting for specific I/O device

Long-term scheduler (job scheduler): decides which jobs enter the ready queue (admission control — mostly gone in modern systems). Short-term scheduler (CPU scheduler): picks which ready process gets the CPU next (runs every ~1–10 ms). Medium-term scheduler: swaps processes in/out of memory (handles degree of multiprogramming).