4.2.1Operating Systems

OS roles — resource management, hardware abstraction, protection

2,468 words11 min readdifficulty · medium

WHY does an OS even exist?

WHY (first principles): Imagine a computer with NO OS. Every program must directly:

  • Talk to the disk controller (different code for every disk model).
  • Decide when to give the CPU to another program (but a buggy program never will).
  • Read/write any memory address (so program A can scribble over program B).

This is unsafe, non-portable, and chaotic. The OS is the trusted referee that owns the hardware so applications don't have to.

Figure — OS roles — resource management, hardware abstraction, protection

Role 1 — Resource Management

WHAT it manages: CPU time, memory, storage, I/O devices, network bandwidth.

HOW (CPU example): The CPU can run only one instruction stream at a time per core. The OS gives each process a tiny time slice (e.g. 10 ms), then a hardware timer interrupt fires, control returns to the OS, and it picks the next process. Switching this fast creates the illusion that all programs run at once.


Role 2 — Hardware Abstraction

WHAT abstractions you already use:

Physical reality OS abstraction
Magnetic sectors / SSD flash blocks File (open/read/write/close)
Physical RAM chips + paging Virtual address space
CPU cores + timer Process / thread
Network card registers Socket

HOW: Your code calls read(fd, buf, n). The OS translates this through the file system → block layer → device driver → controller registers. You never know if it was an SSD, HDD, or USB stick. Swap the disk, swap the driver — your program is unchanged. That's portability.


Role 3 — Protection (and Isolation)

WHY hardware help is required: Software alone can't enforce this — a malicious program could just skip the checks. So the CPU itself refuses privileged instructions in user mode and traps to the OS.

HOW it works (the mechanisms):

  • Dual-mode operation: a mode bit in the CPU. Privileged instructions (set timer, do I/O, modify page tables) fault if attempted in user mode.
  • Memory protection: the MMU checks every address against the process's page table. Out-of-bounds → page fault → OS kills the process (the classic segmentation fault).
  • System calls: the only legal door into the kernel. The app sets up arguments, executes a syscall/trap instruction, CPU switches to kernel mode, OS validates the request, does it, returns.

How the three roles interlock



Recall Feynman: explain to a 12-year-old

Imagine one PlayStation and ten kids who all want to play, and some of them are bullies. A fair referee (the OS) does three things:

  1. Takes turns (resource management): everyone gets 10 seconds, then the next kid — so fast it feels simultaneous. While one kid stares at the menu (I/O wait), the referee lets someone else play, so the console is never just sitting idle.
  2. Universal remote (abstraction): the kids press the same buttons whether it's a PS4 or PS5 or a TV with weird ports. The referee hides the wiring.
  3. Bodyguard (protection): if a kid tries to grab someone else's save file or hold the controller forever, the referee instantly takes it away. The kids physically can't break the rules because the console hardware backs the referee up. That referee is the operating system.


Flashcards

What are the three core roles of an OS?
Resource management, hardware abstraction, and protection (mnemonic: RAP).
Why does an OS need to do resource management?
Hardware (CPU, RAM, I/O) is limited and shared by mutually-untrusting programs; the OS multiplexes it fairly and efficiently.
CPU utilization formula for n processes each waiting fraction p of the time
U=1pnU = 1 - p^n (idle only when all n are simultaneously waiting).
Why does adding more I/O-bound processes raise CPU utilization?
Because pnp^n (probability all are waiting) shrinks fast, so the CPU is rarely idle.
What is hardware abstraction?
A simplified, uniform interface (file, process, socket, virtual memory) that hides device-specific details so apps use the same operations on any hardware.
What hides device-specific details behind the OS's uniform interface?
The device driver.
What two CPU modes enable protection?
User mode (restricted) and kernel mode (privileged); only the OS runs in kernel mode.
What is the only legal way for a user program to enter the kernel?
A system call / trap instruction (a controlled mode switch).
What hardware unit enforces memory protection on every access?
The MMU, by checking addresses against the process's page table.
Why can't a user process disable the preemption timer?
Setting the timer is a privileged instruction; attempting it in user mode traps to the OS instead of executing.
True parallelism vs the concurrency illusion on one core?
One core does fast time-slicing (concurrency illusion); true parallelism requires multiple cores.
Who sets protection policy vs who enforces the mechanism?
OS sets policy; hardware (mode bit + MMU) enforces the mechanism.

Connections

  • Processes and Threads — the abstraction the scheduler manages.
  • CPU Scheduling — the concrete algorithms of Role 1.
  • Virtual Memory and Paging — abstraction + protection of RAM via the MMU.
  • System Calls and Traps — the protected gate between user and kernel.
  • Interrupts and the Timer — the mechanism that lets the OS reclaim the CPU.
  • Device Drivers and I/O — where hardware abstraction is implemented.
  • Kernel vs User Mode — the hardware basis of protection.

Concept Map

creates need for

role 1

role 2

role 3

time-multiplex

space-multiplex

enforced by

enables

raises

derived from

Many programs share one machine, mutual distrust

Operating System, trusted referee

Resource Management

Hardware Abstraction

Protection

CPU Scheduling, time slice

Timer interrupt

Memory and disk allocation

Utilization U = 1 minus p to n

Multiprogramming

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho ek hi computer hai aur usme bahut saare programs ek saath chalna chahte hain — aur ye programs ek dusre par bharosa nahi karte. Bas yahi ek tension se OS ke teen kaam nikalte hain. Pehla: resource management — CPU, RAM, disk limited hain, to OS sabko time-slice deke baari-baari chalata hai, itni fast ki lagta hai sab ek saath chal rahe hain. Jab ek program I/O ka wait kar raha ho, OS doosre ko CPU de deta hai, isliye CPU idle nahi baithta. Yahi reason hai ki zyada I/O-heavy programs load karne se CPU utilization badhta hai — formula U=1pnU = 1 - p^n.

Doosra kaam: hardware abstraction. Duniya mein hazaar tarah ke disk, GPU, network card hain. Agar har app ko har device ka code likhna pade to software banega hi nahi. To OS ek clean interface deta hai — file, process, socket, virtual memory — aur asli device ka mess driver ke peeche chhupa deta hai. Tum sirf open/read/write likhte ho, neeche HDD ho ya SSD, tumhe farak nahi padta. Yahi portability hai.

Teesra kaam: protection. CPU ke do mode hote hain — user mode (limited) aur kernel mode (full power, sirf OS). User program seedha hardware ko ya doosre program ki memory ko touch nahi kar sakta; uska sirf ek darwaaza hai — system call (trap). Memory ke har access ko MMU check karta hai page table se; galat address pe gaye to segmentation fault. Important baat: policy OS decide karta hai, par enforce hardware karta hai (mode bit + MMU). Isiliye koi rogue program OS ko ignore karke timer disable nahi kar sakta — aur isi wajah se Role 1 (sharing) tabhi possible hai jab Role 3 (protection) ho. Yaad rakho: RAP — Resource, Abstraction, Protection.

Go deeper — visual, from zero

Test yourself — Operating Systems

Connections