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
| Field | Contents |
|---|---|
| PID | Unique process identifier |
| State | Running, Ready, Waiting, … |
| Program Counter | Address of next instruction |
| CPU Registers | All general-purpose + status registers |
| Memory Info | Base/limit registers or page table pointer |
| I/O Status | List of open files, pending I/O |
| Accounting | CPU time used, priority, scheduling data |
| Parent PID | PPID of creator |
Process States
admit
New ──────────→ Ready ←──────── Waiting
│ I/O done ↑
dispatch│ │ I/O request
↓ │
Running ──────────┘
│
exit / abort
↓
Terminated| State | Meaning |
|---|---|
| New | Being created |
| Ready | In memory, waiting for CPU |
| Running | Instructions executing on CPU |
| Waiting (Blocked) | Waiting for event (I/O, signal) |
| Terminated | Finished; 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 spaceexec*()replaces the process image (code, data, stack) — PID stays the same- On Windows:
CreateProcess()combines fork + exec in one call
fork() Return Values
| Context | Return value |
|---|---|
| Parent process | Child's PID (> 0) |
| Child process | 0 |
| Error | −1 |
Process Termination
| Cause | Mechanism |
|---|---|
| Normal exit | exit(status) / return from main |
| Error exit | exit(1), uncaught exception |
| Fatal signal | SIGSEGV, SIGKILL, … |
| Killed by parent | kill(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)
| Mechanism | Kernel involvement | Direction | Use case |
|---|---|---|---|
| Pipe (anonymous) | Yes | Unidirectional | Parent ↔ child |
| Named pipe (FIFO) | Yes | Unidirectional | Unrelated processes |
| Message queue | Yes | Both | Structured messages |
| Shared memory | Setup only | Both | Highest throughput |
| Socket | Yes | Both | Network or local |
| Signal | Yes | One-way notification | Async events |
| mmap (file-backed) | Setup only | Both | Large 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:
- Save A's CPU registers into A's PCB
- Update A's state (Ready or Waiting)
- Load B's PCB registers into CPU
- Update B's state to Running
- 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
| Queue | Contents |
|---|---|
| Job queue | All processes in the system |
| Ready queue | Processes in memory, ready to run |
| Device queues | Processes 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).