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.
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)
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.
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)
03System Services, Linkers & Loaders
"System services" are the helper programs shipped with the OS. And every executable you run went through a compiler → linker → loader chain.
| Linking | When library code joins | Result |
|---|---|---|
| Static | At compile/link time | Bigger file, runs anywhere |
| Dynamic | At run time | Small file, shares one library copy in RAM |
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.
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.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).
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).
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.
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.
Thread vs Process — exam table
| Process | Thread | |
|---|---|---|
| Creation | Heavy (copy PCB, memory) | Light (just a stack + registers) |
| Memory | Isolated | Shared within the process |
| Fault | Dies alone | Can kill the whole process |
| Speed | Slower context switch | Faster 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)
| Process | Arrival (AT) | Burst (BT) | Priority (1=highest) |
|---|---|---|---|
| P1 | 0 | 1 | 2 |
| P2 | 0 | 7 | 1 |
| P3 | 2 | 4 | 4 |
| P4 | 2 | 3 | 3 |
TAT = completion − arrival · WT = TAT − burst. Lower average WT is better.
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
| Quantity | Formula |
|---|---|
| Turnaround time | TAT = completion − arrival |
| Waiting time | WT = TAT − burst |
| Avg TAT/WT | sum over processes ÷ n |
| CPU utilisation | busy time ÷ total time |
| Throughput | completed processes ÷ time |
| Fork count | 2ⁿ processes for n forks |
| Response time | first response − arrival |