Operating Systems — watch the machine think

Unit-I: what the OS does, how processes are born and scheduled, and how threads and IPC work. The centrepiece is a live CPU scheduler — run FCFS, SJF, SRTF, Priority or Round-Robin and watch the Gantt chart draw itself.

AMBER TERMINAL THEME · SCANLINES · PHOSPHOR GLOW
$ ./boot --unit1 --interactive … loading processes → linking modules → spinning up scheduler █

01OS Structure — what the OS actually does

An OS is an intermediary between user programs and hardware. It provides services that make programs safe, convenient and efficient.

The nine services, grouped

User-facing

  • User interface (CLI / GUI)
  • Program execution
  • I/O operations
  • File-system manipulation

System-facing

  • Communications (between processes)
  • Error detection & handling
  • Resource allocation

Safety & accounting

  • Protection & security
  • Accounting (tracking usage)
✦ Exam point The exam loves: "List the services of an OS". Say it in three rows of three: UI, execution, I/O, files · comms, errors, allocation · protection, accounting.

02User Interface & System Calls

Programs cannot touch hardware directly — they ask the kernel through system calls. A trap switches the CPU from user mode to kernel mode; the mode bit records which.

System call trace — watch the mode bit flip↻ replay
read() is just a number. The user program loads the syscall number + args, executes trap, the CPU jumps to kernel code, the mode bit flips to kernel (0), the work happens, and control returns to user mode (1).

Passing parameters (3 methods)

  • Registers (fast, few args)
  • Block / table in memory, pointer in a register
  • Push onto the stack

Six categories of system calls

  • Process control (fork, exit)
  • File management (open, read, write)
  • Device management (request, release)
  • Information maintenance (get/set time)
  • Communications (send, receive, pipe)
  • Protection (set permission)
⚠ Common mistake The mode bit is 0 = kernel, 1 = user. User programs are not allowed to set the bit to 0 — only the trap/interrupt mechanism can — otherwise any program could seize the machine.

04Process Concept — the heart of the OS

A process is a program in execution. Its identity lives in the PCB (Process Control Block): state, program counter, registers, memory limits, open files.

One lifetime through the five states↻ replay
new →(admit)→ ready →(dispatch)→ running →(I/O or event wait)→ waiting →(I/O complete)→ ready →(exit)→ terminated. A running process can also be preempted back to ready (time slice ends).
✓ Worked — fork counts One parent calls fork() n times: total processes = 2ⁿ (parent + children). With n=3: 8 processes total, 7 children. Every child starts executing right after the fork() that created it — not at the top of the program.
A fork tree — each fork doubles the family↻ replay
P0 forks P1; both fork P2 and P3… the tree grows level by level. The 2ⁿ rule falls out of the pattern.

05Schedulers & Queues

Processes wait in queues. Three schedulers pick which process moves where.

Long-term (job)

Selects which jobs enter the ready queue. Controls the degree of multiprogramming. Runs rarely.

Short-term (CPU)

Selects the next process to run from ready. Runs very frequently (every ~10 ms) — must be fast.

Medium-term

Swaps processes in/out of memory to balance the load (suspend/resume).

✦ Exam point The short-term scheduler is the "CPU scheduler" everyone means in chapter 6. A context switch saves the old process's state into its PCB and loads the new one's — that overhead is pure cost, so it must be kept tiny.

06Interprocess Communication (IPC)

Two cooperating processes exchange data two ways: shared memory (both read/write one region) or message passing (kernel shuttles messages, no shared space).

Shared memory vs message passing↻ replay
Left: producer writes a number into shared RAM, consumer reads it — fast, but both must synchronise. Right: the kernel copies a message from one process to the other's mailbox — safe, slower.

Bounded buffer (classic producer–consumer)

The buffer holds N items. Producer must wait if full; consumer must wait if empty. A full solution needs semaphores/monitors — the raw shared buffer alone races.

⚠ Common mistake Shared memory is not synchronised by itself. Two processes writing the same cell simultaneously lose data. The kernel only sets up the region; the programs must add locks. Message passing moves the synchronisation burden to the kernel.

07Threads & Multithreading Models

A thread is a lightweight process: shares the process's code, data and files, but has its own stack, registers and program counter. Threads of one process share memory — cheaper than processes.

Three multithreading models — how user threads map to kernel threads↻ replay
Many-to-one: all user threads share one kernel thread (no parallelism, one blocks all). One-to-one: 1:1 (parallel, but costly). Many-to-many: multiplexed — parallelism without blowing up the kernel thread count.

Thread vs Process — exam table

ProcessThread
CreationHeavy (copy PCB, memory)Light (just a stack + registers)
MemoryIsolatedShared within the process
FaultDies aloneCan kill the whole process
SpeedSlower context switchFaster context switch

08CPU Scheduling — the live lab

The scheduler picks the next process to run. Criteria: CPU utilisation, throughput, turnaround time (TAT), waiting time (WT), response time. Watch each algorithm run on this fixed set:

The process set (fixed for honest comparison)

ProcessArrival (AT)Burst (BT)Priority (1=highest)
P1012
P2071
P3244
P4233

TAT = completion − arrival · WT = TAT − burst. Lower average WT is better.

Run an algorithm — the Gantt chart executes in real time
choose an algorithm →
Every segment you see is computed live from the same four processes. Note how SRTF preempts P2 twice and achieves the best average waiting time (2.75).

Why the averages differ

  • FCFS (4.25): long P2 delays P3, P4 — the convoy effect.
  • SJF (4.00): shortest first minimises average WT when non-preemptive.
  • SRTF (2.75): preempts P2 the instant shorter P4/P3 arrive — best here.
  • Priority (5.50): P1 (prio 2) waits behind P2 (prio 1) — fast ≠ fair.
  • RR (5.00): quantum=2 adds many switches; P2 gets dribbled.

Concepts to pair with each

  • Convoy effect: short jobs stuck behind one long job (FCFS).
  • Starvation: low-priority jobs may never run (Priority). Solved by aging.
  • Quantum tuning: too small → too many switches; too large → behaves like FCFS (RR).
  • Thread scheduling: PCS (process-contention scope) vs SCS (system-contention scope).

09Formula Sheet & 5 Near-Certain Questions

QuantityFormula
Turnaround timeTAT = completion − arrival
Waiting timeWT = TAT − burst
Avg TAT/WTsum over processes ÷ n
CPU utilisationbusy time ÷ total time
Throughputcompleted processes ÷ time
Fork count2ⁿ processes for n forks
Response timefirst response − arrival
Q1 · List the OS services in their three groups.
User: UI, program execution, I/O, file management · System: communication, error handling, resource allocation · Safety: protection/security, accounting.
Q2 · Trace a read() system call through the mode bit.
User loads syscall no. + args → trap → mode bit 1→0 → kernel reads → returns → mode bit 0→1.
Q3 · What is the process tree after 3 forks from one parent?
2³ = 8 processes total, 7 children. The tree doubles each level.
Q4 · Compute avg WT for SJF on (P1:AT0,BT1)(P2:AT0,BT7)(P3:AT2,BT4)(P4:AT2,BT3).
Order P1,P2,P4,P3. WT = 0,1,6,9 → avg 4.00. Run the lab above to confirm.
Q5 · Why does Priority scheduling risk starvation and how is it fixed?
Low-priority jobs can wait forever. Fix: aging — gradually increase a waiting process's priority.