💻 Computer Science · Operating Systems

OS tricks that make processes click

Processes, memory, and scheduling — demystified.

🖥️ OS

Memory tricks

Proven mnemonics — fast to learn, hard to forget.

Process Life Cycle
Process states: New Ready Running Waiting Terminated
Process Life Cycle
Five states a process moves through during execution
New: being created. Ready: waiting for CPU. Running: executing. Waiting: blocked (e.g., waiting for disk). Terminated: finished. Scheduler moves processes between Ready and Running.
New
Being created
Ready
Waiting for CPU
Running
Executing on CPU
Waiting
Blocked on I/O
Terminated
Finished
Deadlock Conditions
Deadlock requires all 4: Mutual exclusion, Hold-and-wait, No preemption, Circular wait
Deadlock Conditions
Four conditions that must ALL be present for a deadlock
Remove any one condition → prevent deadlock. Mutual exclusion: only one process uses resource at a time. Hold and wait: holds resource while waiting for another. No preemption: can't forcibly take resources. Circular wait: chain of processes each waiting on the next.
M
Mutual exclusion
H
Hold and wait
N
No preemption
C
Circular wait
Virtual Memory
Virtual memory: OS swaps pages to disk — programs see more RAM than exists
Virtual Memory
The OS gives programs the illusion of abundant memory
OS uses disk as overflow when RAM fills up. Programs see a large contiguous address space. Page fault: needed page not in RAM → load from disk (slow). Paging: fixed-size blocks.
Scheduling Algorithms
CPU Scheduling: FCFS (First Come First Served), SJF (Shortest Job First), Round Robin (fixed time quantum), Priority (highest priority runs first)
Scheduling Algorithms
How the OS decides which process gets CPU time
FCFS: first come first serve — simple, long jobs block short ones. SJF: shortest job first — minimizes wait, needs job-length prediction. Round Robin: each gets a fixed time slice — fair. Priority: highest priority runs first.
Thrashing
Thrashing: too many processes → constant page swapping → CPU spends all time on I/O, none on work
Thrashing
When virtual memory page swapping overwhelms the CPU
Occurs when the total working set of all processes exceeds available RAM. OS spends more time swapping pages than running processes. Solution: reduce multiprogramming degree or add RAM.
File Systems
File systems: FAT32 (File Allocation Table 32-bit, simple and compatible), NTFS (New Technology File System, Windows journaling), ext4 (Linux), APFS (Apple File System) (Apple)
File Systems
How operating systems organize and store data on disk
File system: organizes data on storage devices. FAT32: simple, max 4GB file size, universal compatibility. NTFS: Windows, journaling (recovers from crashes), large files, permissions. ext4: Linux standard, journaling, large file support. Hierarchical structure: root → directories → files. Inodes: metadata about files (not content).
Memory Hierarchy
Memory hierarchy: registers → cache (L1/L2/L3) → RAM → SSD → HDD. Faster = smaller and more expensive.
Memory Hierarchy
The pyramid of memory — speed vs capacity vs cost
Registers: fastest, inside CPU, tiny (bytes). L1 cache: ~32KB per core, ~1ns. L2 cache: ~256KB, ~4ns. L3 cache: shared, ~8MB, ~40ns. RAM: ~16GB typical, ~100ns. SSD: ~1TB, ~100μs. HDD: ~4TB, ~10ms. OS and hardware move data up the hierarchy (caching) to match speed with usage patterns.
Context Switching
Context switch: OS saves current process state, loads another process state — enables multitasking
Context Switching
How the OS rapidly switches between processes to simulate multitasking
When CPU switches from one process to another: save current process's registers, program counter, and state to PCB (Process Control Block). Load next process's saved state. Context switches are expensive (~microseconds) — too many degrade performance. Threads have lighter context switches than processes.
Semaphores and Synchronization
Semaphore: integer variable for synchronization. Mutex (Mutual Exclusion lock, binary semaphore). P() = wait. V() = signal.
Semaphores and Synchronization
How processes coordinate access to shared resources
Semaphore: integer variable — P() decrements (wait if 0), V() increments (signal). Binary semaphore (mutex): 0 or 1, implements mutual exclusion. Counting semaphore: tracks available resources. Dining philosophers problem: classic deadlock scenario. Monitor: higher-level synchronization construct.
Paging
Paging: divide memory into fixed-size pages. Page table maps virtual to physical addresses.
Paging
How virtual memory is divided into manageable chunks
Page: fixed-size block of virtual memory (typically 4KB). Frame: corresponding physical memory block. Page table: maps virtual page numbers to physical frame numbers. Page fault: accessing page not in RAM → OS loads from disk → slow. Working set: set of pages process needs — keep in RAM to minimize page faults.
I/O Management
I/O (Input/Output) management: buffering (temporary storage), spooling (queue for slow devices), interrupts (device signals CPU)
I/O Management
How the OS handles input and output efficiently
Busy waiting (polling): CPU repeatedly checks if I/O is done — wastes CPU cycles. Interrupts: device signals CPU when I/O complete — CPU does other work meanwhile. DMA (Direct Memory Access): device transfers data directly to memory without CPU involvement. Buffering: temporary storage to handle speed mismatch.
Shell and Scripting
Shell: command-line interface to OS. Script: sequence of commands in a file. Pipe: output of one command as input to next.
Shell and Scripting
The command-line interface and automation
Shell: program that interprets commands — bash, zsh, PowerShell. Pipe (|): chain commands, output of one → input of next. cat file | grep 'error' | wc -l. Redirection: > (output to file), < (input from file), >> (append). Shell scripts: automate repetitive tasks. Environment variables: KEY=value, PATH determines where programs are found.
Mnemonic
What it means
00📚 0 left

No saved cards yet — click ☆ Save on any memory trick.

🎓 Common Exam Questions
Q: Four deadlock conditions and how to prevent deadlock.
A: All four Coffman conditions required: (1) Mutual exclusion — resource held exclusively. (2) Hold-and-wait — process holds resource while waiting for more. (3) No preemption — resources cannot be forcibly taken. (4) Circular wait — cycle where each process waits for the next. Prevention: eliminate one condition. Prevent hold-and-wait by requiring all resources upfront. Prevent circular wait with total ordering on resources. Allow preemption so resources can be reclaimed. Banker's algorithm avoids deadlock proactively.
Q: Compare FCFS, SJF, Round Robin, and Priority scheduling.
A: FCFS (First Come First Served): simple, convoy effect — short jobs behind long ones. SJF (Shortest Job First): optimal average wait theoretically, requires knowing burst time, causes starvation. Round Robin: time quantum per process, fair, no starvation, too small a quantum causes excessive context switching. Priority: highest priority runs first, can starve low-priority — fix with aging. Modern OSes use multilevel feedback queues combining these approaches.
Q: What is virtual memory? Explain paging and page faults.
A: Virtual memory gives each process the illusion of its own large contiguous memory. Paging: physical memory divided into fixed-size frames, logical memory into same-size pages. Page table maps virtual to physical. Demand paging loads pages only when accessed. Page fault: process accesses page not in RAM — OS finds it on disk, loads it into a free frame evicting another if needed using LRU or Clock algorithm, updates page table, process resumes. Many page faults cause thrashing. TLB (Translation Lookaside Buffer) caches recent page table entries for fast translation.
Q: Semaphores and mutexes — explain the producer-consumer problem.
A: Race condition: threads accessing shared data concurrently produce incorrect results. Mutex (Mutual Exclusion lock): binary lock, only acquiring thread can release. Semaphore: integer variable. wait() decrements — if negative, block. signal() increments — unblock waiting thread. Counting semaphore controls N resources. Producer-consumer: mutex protects buffer access; counting semaphore empty (initialized to buffer size) tracks free slots; counting semaphore full (initialized to 0) tracks available items. Producer: wait(empty), lock mutex, add item, unlock, signal(full).
Q: Describe the memory hierarchy and why cache matters.
A: Registers about 1 cycle, L1 Cache about 4 cycles at 32 to 64KB per core, L2 about 12 cycles, L3 about 40 cycles shared, RAM about 100 cycles, SSD about 100,000 cycles, HDD about 10 million cycles. Cache exploits temporal locality (recently used data likely used again) and spatial locality (nearby data likely used soon). Cache miss to RAM is 100 times slower than hit; to SSD is 1000 times; to HDD is 10,000 times. Good cache behavior can matter more than algorithmic complexity for practical input sizes.
Live group chat — up to 8 students per room