OS Complete Notes
OS Complete Notes
Legend: Orange boxes = exam focus | Green boxes = analogies | Blue boxes = important notes | Purple text = book reference
Chapter 1: Introduction to Operating Systems
ANALOGY: Think of the OS like a restaurant manager. The kitchen (hardware) does the actual cooking.
The customers (user/apps) place orders. The manager (OS) coordinates everything — takes orders, assigns
cooks, serves food — so the customer never directly touches the stove.
[Dinosaur Book Ref] Chapter 1, Section 1.1 — "An OS is a program that manages computer hardware."
Goals of an OS:
• Execute user programs and solve user problems
• Make the computer system convenient to use
• Use hardware in an efficient manner
Operating System Controls & coordinates hardware use Windows, Linux, macOS
Application ProgramsDefine how resources are used to solve user problemsWord, Chrome, Games
[Dinosaur Book Ref] Figure 1.1, Chapter 1 — "Abstract View of Computer System Components"
ANALOGY: Imagine you are studying (CPU doing work). Your phone rings (interrupt). You save your place in
the book (save CPU state), answer the call (handle interrupt), then return to studying (resume work). That's
exactly what the OS does!
Types of Interrupts:
Type Cause Example
Hardware Interrupt A device signals the CPU Keyboard press, disk read done
Software Interrupt
Generated by software or errors Division by zero, system call
(Trap/Exception)
Caching:
Caching = copying data from slow storage into faster storage temporarily. The CPU checks cache first; if data is
there (cache hit), great. If not (cache miss), it fetches from slower storage and stores in cache.
ANALOGY: Caching is like keeping your most-used books on your desk (cache = desk, library = main
memory). You grab from the desk first — much faster!
ANALOGY: DMA is like hiring a moving company (DMA) to move furniture (data) directly from store to house
(memory). The owner (CPU) doesn't carry each piece — just gets one call when the job is done.
With Multiprogramming: Job A [===] Job B [===] Job A [===] Job C [===]
CPU NEVER IDLE!
[Dinosaur Book Ref] Section 1.4.1 — "Multiprogramming increases CPU utilization"
Who runs in it? User applications (Word, Chrome) Operating System itself
NOTE: The mode bit is a single hardware bit. When 0 = kernel mode, when 1 = user mode. The hardware
enforces this — user programs physically cannot execute privileged instructions.
1.10 Timer
The OS uses a hardware timer to prevent any single process from hogging the CPU forever.
Hardware Resources CPU, RAM, I/O devices, Disk Allocate & schedule efficiently
Software Resources Files, Processes, Semaphores, Locks Manage access & synchronization
EXAM FOCUS: Name two hardware resources and two software resources — asked in FALL 2024
Midterm!
• Hardware: CPU, Main Memory (RAM), I/O devices (keyboard, disk)
• Software: Files, Processes, Semaphores, Message queues
Peer-to-Peer (P2P) All nodes are equal — both client and server (e.g., BitTorrent)
1.13 Virtualization
Virtualization allows one physical computer to run multiple OS instances simultaneously. A Virtual Machine
Monitor (VMM) or Hypervisor manages this.
ANALOGY: Virtualization is like building apartments (virtual machines) inside one big building (physical
hardware). Each apartment thinks it has its own kitchen and bathroom, but they're sharing the building's real
plumbing.
• Benefits: Run multiple OSes, testing, isolation, server consolidation
• Examples: VMware, VirtualBox, Hyper-V
[Dinosaur Book Ref] Section 1.7 — Virtualization
Chapter 2: Operating-System Services & Structure
2.1 OS Services
The OS provides services both for users and for the system itself:
Service Description
File-System Manipulation Create, delete, read, write, search files & directories
Error Detection Detect and recover from CPU, memory, I/O errors
A system call is the programming interface between a user program and the OS kernel. It is how user
programs request services from the OS.
ANALOGY: A system call is like a waiter in a restaurant. You (user program) cannot go into the kitchen
(kernel) yourself. You tell the waiter (system call) what you want, and they bring it to you. The waiter is the
only one allowed in the kitchen.
1. Registers
Parameters placed directly in CPU registers before
Limited
system
by number
call of registers (usually ~6-8)
2. Block/Table
Parameters
in Memory
stored in a memory block; address of block placed
None
in a—
register
used by Linux & Solaris
3. Stack
Parameters pushed onto program stack by user; OSNone
pops—them
flexible
off for any number of params
NOTE: Methods 2 (block) and 3 (stack) do NOT limit the number or length of parameters. Method 1 (registers) is
simplest but limited.
[Dinosaur Book Ref] Section 2.3.1 — Parameter Passing
Types of System Calls:
• Process Control: create process, terminate process, wait, allocate memory
• File Management: create/delete file, open/close, read/write
• Device Management: request/release device, read/write device
• Information Maintenance: get/set time, get system data
• Communications: create connection, send/receive messages
• Protection: get/set permissions, control access
2. Layered Approach:
OS divided into layers. Layer 0 = hardware. Layer N = user interface. Each layer uses services only from the
layer directly below it.
Layer N ■ User Interface
Layer N-1 ■ I/O Management
Layer N-2 ■ Process/Memory Management
... ■ ...
Layer 1 ■ Hardware Abstraction
Layer 0 ■ HARDWARE
• Advantage: Easy to debug and verify — each layer can be tested independently
• Advantage: Simple design — each layer only interacts with adjacent layers
• Disadvantage: Performance overhead — requests must pass through many layers
• Disadvantage: Difficult to define layer boundaries cleanly
[Dinosaur Book Ref] Section 2.7.2 — Layered Approach
Layered Layers, each uses layer below Easy to debug Slow, hard to define layers
Microkernel Minimal kernel, rest in user space Secure, reliable Performance overhead
A Virtual Machine (VM) is a software emulation of a physical computer. It lets you run a guest OS inside a host
OS. The VMM (Virtual Machine Monitor / Hypervisor) manages this.
ANALOGY: A VM is like a TV show set. It looks like a real office/kitchen/hospital (guest OS), but it's all built
inside a studio (host OS). The director (VMM) controls everything.
ANALOGY: A recipe (program) sitting in a cookbook is passive. When a chef (CPU) follows that recipe to
cook a meal (process), it becomes active. Multiple chefs can follow the same recipe simultaneously — just
like multiple processes can run from one program.
[Dinosaur Book Ref] Section 3.1 — Process Concept
ANALOGY: The PCB is like your student file at university. It has your name, ID, grades, courses, outstanding
fees — everything about you. The admin (OS) checks your file whenever they need to deal with you (your
process).
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■
■ 5-STATE PROCESS MODEL ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■
■■■■■■■ admitted ■■■■■■■■■ scheduler ■■■■■■■■■■■
■ NEW ■ ■■■■■■■■■■■ ■ READY ■ ■■dispatch■■■ ■ RUNNING ■ ■■■■ exit ■■■■■ TERM
INATED
■■■■■■■ ■■■■■■■■■ ■■■■■■■■■■■
▲ ■
■ interrupt/ ■ I/O or event
■ time quantum ■ wait
■ expires ▼
■ ■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■ ■ WAITING ■
I/O complete ■■■■■■■■■■■
event occurs
State Transition Table (asked in FALL 2023 & FALL 2024 Midterms!):
EXAM FOCUS: Given an event, identify starting state and ending state — appears as a table
question!
A context switch happens when the CPU switches from running one process to another.
Stack NO — each has own copy Each process has its own stack
NOTE: On fork(): Stack is NOT shared (each has its own). Heap uses "Copy-on-Write" — copied only when
modified. Shared memory segments remain shared.
[Dinosaur Book Ref] Section 3.3.1 — Process Creation
Processes may need to communicate. There are two main IPC models:
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■
■ (a) SHARED MEMORY ■ (b) MESSAGE PASSING ■
■ ■ ■
■ Process A ■ SHARED ■ Process B ■ Process A ■■msg■■■ Process B ■
■ ■ MEMORY ■ ■ ■■■msg■■ ■
■ Both read/write a common ■ OS delivers messages between ■
■ area of memory. ■ processes (like email). ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■
Why slower? N/AEach send/receive requires a system call (mode switch to kernel and back)
Best for Large data, same machine Small messages, different machines
Pipes:
• Ordinary (Anonymous) Pipes: Unidirectional, require parent-child relationship
• Named Pipes: Bidirectional, no parent-child needed, multiple processes can use it
ANALOGY: A pipe is literally a one-way pipe: data flows in one end and out the other, just like water in a
plumbing pipe.
Thread switching is normally faster than process switching because threads share the same address space (no
need to switch memory maps, page tables, etc.).
• Thread switching == Process switching time when: threads belong to different processes
• In this case, the OS must switch address spaces just like a full process switch
• Within the same process, thread switch is cheaper (same memory space, same open files)
Concurrency Parallelism
Definition
Multiple tasks making PROGRESS (not necessarily
Multiple tasks
at same
executing
instant)SIMULTANEOUSLY (at the same instant)
Analogy
One chef cooking multiple dishes by switching
Multiple
between
chefsthem
each cooking a different dish at the same time
ANALOGY: A process is like a factory. Threads are like workers inside that factory. They all share the same
building (memory), tools (resources), and blueprints (code), but each worker (thread) does their own task
simultaneously.
Open files & I/O resources Stack (local variables, function calls)
Data
The
Parallelism
SAME operation is performed on different
Summing
SUBSETS
a large
of data
array:
simultaneously
Core 1 sums first half, Core 2 sums second half
DIFFERENT
Task Parallelism
operations (tasks/threads) run simultaneously
One thread spells-check,
on the sameanother
or different
thread
data
auto-saves, another syncs to cloud
ANALOGY: Data Parallelism: 4 workers all painting walls — same job, different walls. Task Parallelism:
Worker 1 paints, Worker 2 lays flooring, Worker 3 installs windows.
[Dinosaur Book Ref] Section 4.2.1 — Types of Parallelism
In fork-join, a parent thread forks (creates) multiple child threads to do work in parallel, then joins (waits for) all
children to complete before continuing.
Parent Thread
■
■■■■ fork ■■■ Child Thread 1 (does task 1)
■■■■ fork ■■■ Child Thread 2 (does task 2)
■■■■ fork ■■■ Child Thread 3 (does task 3)
■
■■■■ join ■■(waits for all children to finish)
■
Parent continues with combined result
In NUMA systems, each CPU has its own local memory that it can access faster than memory belonging to
other CPUs.
■■■■■■■■■■■■ fast ■■■■■■■■■■■■ ■■■■■■■■■■■■ fast ■■■■■■■■■
■■■
■ CPU 0 ■ ■■■■■■■■ ■ Memory 0 ■ ■ CPU 1 ■ ■■■■■■■■ ■ Memory 1 ■
■■■■■■■■■■■■ ■■■■■■■■■■■■ ■■■■■■■■■■■■ ■■■■■■■■■
■■■
■ ■ ■ ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ (slow cross-access)
Thread Cancellation:
• Asynchronous cancellation: Target thread terminated immediately — risky
• Deferred cancellation: Thread checks at safe points if it should cancel — safer
Chapter 5: CPU Scheduling
• CPU Burst: Time a process uses the CPU before needing I/O
• I/O Burst: Time a process waits for I/O to complete
• Processes alternate between CPU bursts and I/O bursts throughout their life
ANALOGY: CPU scheduling is like a doctor's appointment system. The doctor (CPU) can only see one
patient at a time. The scheduling algorithm decides which patient (process) gets to go in next.
[Dinosaur Book Ref] Section 5.1 — Basic Concepts
Non-Preemptive
Once a process gets CPU, it keeps it until doneOnly
or waiting
when process terminates or does I/O
NOTE: Modern OSes (Windows, Linux, macOS) ALL use preemptive scheduling.
Dispatcher:
The dispatcher is the module that gives control of the CPU to the process selected by the scheduler. It does
context switch, switches to user mode, and jumps to the right program location.
• Dispatch latency = time to stop one process and start another (should be minimal)
NOTE: Waiting time = Response time when: there is no output until full completion (e.g., FCFS, SJF, Priority
non-preemptive, and RR if process runs to completion without preemption). In Round Robin, response time <
waiting time typically.
[Dinosaur Book Ref] Section 5.2 — Scheduling Criteria
Gantt Chart:
|■■■■P1■■■■■■■■■■■■■■■■■■■■■|■P2■|■P3■|
0 24 27 30
Gantt Chart:
|■P4■|■■■■P1■■■■■|■■■■■■■P3■■■■■■|■■■■■■■■P2■■■■■■■■■■|
0 3 9 16 24
EXAM FOCUS: SRTF Gantt chart is asked in nearly every midterm — practice this!
Gantt: |P1|P2■■|P4■■■■|P1■■■■■■|P3■■■■■■■■■■■■■|
0 1 5 10 17 26
EXAM FOCUS: RR Gantt chart with given quantum — calculated in multiple midterms!
Example: P1(burst=24), P2(burst=3), P3(burst=3), arrive t=0, quantum=4
Gantt: |■■P1■■|■P2■|■P3■|■■P1■■|■■P1■■|■■P1■■|■■P1■■|■■P1■■|
0 4 7 10 14 18 22 26 30
EXAM FOCUS: Priority scheduling Gantt chart — preemptive and non-preemptive — asked in FALL
2022, FALL 2023, SPRING 2022, SPRING 2023, FALL 2025 Midterms!
IMPORTANT CONVENTION (check problem statement!):
"Lower number = HIGHER priority" (most common in exam papers)
OR "Lower number = LOWER priority" (sometimes used — READ CAREFULLY!)
Order: P2(p1)→P5(p2)→P1(p3)→P3(p4)→P4(p5)
Gantt: |P2|■■P5■■|■■■■■■P1■■■■■■|■■P3■■|P4|
0 1 6 16 18 19
• Step 1: List all processes with arrival time, burst time, priority
• Step 2: Understand the algorithm: preemptive or non-preemptive? which criterion?
• Step 3: Draw the Gantt chart timeline step by step
• Step 4: Calculate Completion Time (CT) = when process finishes
• Step 5: Calculate Turnaround Time (TAT) = CT − Arrival Time
• Step 6: Calculate Waiting Time (WT) = TAT − Burst Time
• Step 7: Calculate averages: Average WT = sum of all WTs ÷ number of processes
Waiting Time (WT) Turnaround Time − Burst Time OR Start Time − Arrival Time
A Multilevel Feedback Queue has multiple queues with different scheduling algorithms and priorities.
Processes can move between queues based on their behavior.
Queue 0 (Highest Priority): RR with quantum = 8ms
■ if not done in 8ms → move to Queue 1
▼
Queue 1: RR with quantum = 16ms
■ if not done in 16ms → move to Queue 2
▼
Queue 2 (Lowest Priority): FCFS
A race condition occurs when two or more processes/threads access shared data concurrently, and the final
result depends on the order of execution (which is unpredictable). This leads to data inconsistency.
ANALOGY: Two people editing the same Google Doc simultaneously without coordination. Person A reads
"balance = 500", Person B reads "balance = 500". A adds 100 and writes 600. B subtracts 200 and writes
300. The result is 300 — but the correct answer is 400! A's update was lost.
Structure of a process:
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■ do { ■
■ ENTRY SECTION ← ask permission to enter ■
■ ■■■■■■■■■■■■■■■■■■■■■■ ■
■ CRITICAL SECTION ← access shared resource ■
■ ■■■■■■■■■■■■■■■■■■■■■■ ■
■ EXIT SECTION ← announce leaving ■
■ REMAINDER SECTION ← rest of code ■
■ } while(true); ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
1. Mutual Exclusion
If Pi is in its CS, no other process can beOnly
in their
ONE CSprocess in the critical section at a time
If no process
2. Progress
is in CS and some want to enter, selection
If CS cannot
is free and
be postponed
someone wants
indefinitely
in, they should eventually get in
A bound
3. Bounded
must exist
Waiting
on how many times others enter
NoCS
process
after ashould
process
wait
hasforever
requested
— there's
entry a limit to how long you wait
A semaphore is an integer variable that is accessed only through two atomic operations: wait() and signal().
wait(S) { signal(S) {
while (S <= 0) S++;
; // busy wait }
S--;
}
ANALOGY: A semaphore is like a parking lot counter sign. The sign shows available spots (S). When a car
enters (wait), the count decreases. When a car leaves (signal), the count increases. If count = 0, you wait
outside until someone leaves.
Types of Semaphores:
Type Value Range Purpose Equivalent To
Binary Semaphore 0 or 1 only Mutual exclusion (one process at a time) Mutex Lock
Process Pi:
wait(mutex); // enter: decrement, wait if 0
// CRITICAL SECTION
signal(mutex); // exit: increment, wake up waiter
P1: P2:
S1; wait(sync); // wait until signaled
signal(sync); S2; // runs after S1
NOTE: Key insight: Initialize to 0 for ordering/synchronization. Initialize to 1 for mutual exclusion (mutex).
wait(S):
S->value--;
if (S->value < 0) {
add process to S->list; // put to sleep
block(); // suspend process
}
signal(S):
S->value++;
if (S->value <= 0) {
remove process P from S->list; // wake someone up
wakeup(P); // put in ready queue
}
Scenario: Variable Y = 10. Three processes: P1: Y×2, P2: Y+5, P3: Y-8. Binary semaphore (S=1) enforces
strict mutual exclusion. Find max and min final value of Y.
All 6 possible orderings:
P1→P2→P3: (10×2)+5-8 = 17
P1→P3→P2: (10×2)-8+5 = 17
P2→P1→P3: (10+5)×2-8 = 22
P2→P3→P1: (10+5-8)×2 = 14
P3→P1→P2: (10-8)×2+5 = 9
P3→P2→P1: (10-8+5)×2 = 14
Usage:
acquire(lock);
// critical section
release(lock);
NOTE: A binary semaphore (initialized to 1) and a mutex lock serve the same purpose. Mutex = "MUTual
EXclusion". Only one process can hold it at a time.
A producer produces items and puts them in a shared buffer. A consumer takes items from the buffer and
consumes them. The buffer has limited size N.
ANALOGY: Producer = chef making dishes. Consumer = customer eating dishes. Buffer = the serving
counter (max N dishes). Chef waits if counter is full. Customer waits if counter is empty.
Semaphores Needed:
Semaphore Initial Value Meaning
Solution:
PRODUCER: CONSUMER:
while (true) { while (true) {
produce item wait(full); // wait for item
wait(empty); // wait slot wait(mutex); // get exclusive access
wait(mutex); // lock buffer remove item from buffer
add item to buffer signal(mutex); // release lock
signal(mutex);// unlock signal(empty); // signal empty slot
signal(full); // signal item consume item
} }
NOTE: Order matters! Always do wait(empty/full) BEFORE wait(mutex). Doing wait(mutex) first can cause
deadlock!
[Dinosaur Book Ref] Section 7.1 — Bounded-Buffer Problem
• Readers: Only read — multiple readers can read simultaneously (no conflict)
• Writers: Read AND write — only one writer at a time, no readers while writing
ANALOGY: A library's book: Many students (readers) can read the same book at the same time. But when
an author (writer) edits the book, nobody else can read or write until done.
Semaphores:
• rw_mutex = 1: Mutual exclusion for writers (and first/last reader)
• mutex = 1: Protect the read_count variable
• read_count = 0: Number of currently reading processes
WRITER: READER:
while (true) { while (true) {
wait(rw_mutex); wait(mutex);
// WRITE read_count++;
signal(rw_mutex); if (read_count == 1)
} wait(rw_mutex); // first reader locks writer
signal(mutex);
// READ
wait(mutex);
read_count--;
if (read_count == 0)
signal(rw_mutex); // last reader unlocks
signal(mutex);
}
NOTE: First reader blocks writers by doing wait(rw_mutex). Last reader unblocks writers with signal(rw_mutex).
While any reader is reading, writers are blocked.
• Problem: Writers may starve if readers keep arriving (First Readers-Writers problem)
[Dinosaur Book Ref] Section 7.2 — Readers-Writers Problem
• Problem: All 5 philosophers pick up left chopstick simultaneously → all wait for right → DEADLOCK!
Monitor Solution:
States: THINKING, HUNGRY, EATING
pickup(i):
state[i] = HUNGRY;
test(i); // try to eat
if (state[i] != EATING) self[i].wait(); // wait if cannot eat
test(i):
if (left neighbor != EATING) AND (state[i]==HUNGRY) AND (right neighbor != EATING):
state[i] = EATING;
self[i].signal(); // wake up philosopher i
putdown(i):
state[i] = THINKING;
test(left neighbor); // maybe they can eat now
test(right neighbor);
Barrier (all wait for all) S=0 All processes reach barrier, then all continue
To find legal orderings: A process can execute its next operation only if the semaphore value allows it (wait
blocks if S=0, signal always succeeds).
Example: X=0, Y=1, Z=1
P1: wait(Z), print P1, signal(Z)
P2: wait(Z), print P2, signal(X)
P3: wait(Y), print P3, signal(Z)
P4: wait(X), print P4, signal(Y)
Initially: Z=1 (P1 or P2 can start), Y=1 (P3 can start), X=0 (P4 BLOCKED)
NOTE: Algorithm for finding legal orders: 1. List semaphore values initially 2. Find all processes that can run (their
wait semaphore > 0) 3. Pick one, execute it, update semaphore values 4. Repeat until all processes done 5. Each
path through this decision tree is a legal order
[Dinosaur Book Ref] Chapter 6-7, Operating System Concepts 10th Ed. — Synchronization Tools & Examples