4.2.1 · HinglishOperating Systems

OS roles — resource management, hardware abstraction, protection

2,351 words11 min readRead in English

4.2.1 · Coding › Operating Systems


OS exist hi kyun karta hai?

WHY (first principles se): Imagine karo ek computer jisme koi OS nahi hai. Har program ko directly:

  • Disk controller se baat karni padegi (har disk model ke liye alag code).
  • Decide karna padega ki CPU doosre program ko kab dena hai (lekin ek buggy program kabhi nahi dega).
  • Koi bhi memory address read/write karna padega (toh program A, program B par scribble kar sakta hai).

Yeh unsafe, non-portable, aur chaotic hai. OS woh trusted referee hai jo hardware ka maalik hota hai taaki applications ko nahi hona padta.

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

Role 1 — Resource Management

WHAT manage karta hai: CPU time, memory, storage, I/O devices, network bandwidth.

HOW (CPU example): CPU ek time par ek core par sirf ek instruction stream run kar sakta hai. OS har process ko ek chhota sa time slice deta hai (jaise 10 ms), phir ek hardware timer interrupt fire karta hai, control OS ko wapas jaata hai, aur woh agla process choose karta hai. Itni tezi se switch karne se yeh illusion banta hai ki sabhi programs ek saath run kar rahe hain.


Role 2 — Hardware Abstraction

WHAT abstractions aap already use karte ho:

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: Aapka code read(fd, buf, n) call karta hai. OS isko file system → block layer → device driver → controller registers ke through translate karta hai. Aapko kabhi pata nahi chalta ki woh SSD tha, HDD tha, ya USB stick. Disk badlo, driver badlo — aapka program unchanged rehta hai. Yahi portability hai.


Role 3 — Protection (aur Isolation)

Hardware help kyun zaroori hai: Software akela yeh enforce nahi kar sakta — ek malicious program bas checks skip kar sakta hai. Isliye CPU khud user mode mein privileged instructions refuse karta hai aur OS ko trap karta hai.

HOW kaam karta hai (mechanisms):

  • Dual-mode operation: CPU mein ek mode bit. Privileged instructions (timer set karo, I/O karo, page tables modify karo) user mode mein attempt karne par fault karte hain.
  • Memory protection: MMU har address ko process ki page table ke against check karta hai. Out-of-bounds → page fault → OS process ko maarta hai (classic segmentation fault).
  • System calls: kernel mein jaane ka sirf legal darwaza. App arguments set up karta hai, syscall/trap instruction execute karta hai, CPU kernel mode mein switch karta hai, OS request validate karta hai, kaam karta hai, return karta hai.

Teenon roles kaise interlock karte hain



Recall Feynman: ek 12-saal ke bachche ko explain karo

Imagine karo ek PlayStation aur dus bachche jo sab khelna chahte hain, aur kuch bully bhi hain. Ek fair referee (OS) teen kaam karta hai:

  1. Baari leta hai (resource management): sabko 10 second milte hain, phir agla bachcha — itni tezi se ki lagta hai sab ek saath khel rahe hain. Jab ek bachcha menu par ghoorta hai (I/O wait), referee kisi aur ko khelne deta hai, toh console kabhi idle nahi baithti.
  2. Universal remote (abstraction): bacche same buttons dabate hain chahe PS4 ho, PS5 ho, ya weird ports wala TV. Referee wiring chhupata hai.
  3. Bodyguard (protection): agar koi bachcha kisi aur ki save file grab karne ki koshish kare ya hamesha ke liye controller pakad le, referee instantly le leta hai. Bacche physically rules nahi tod sakte kyunki console hardware referee ka saath deta hai. Woh referee hi operating system hai.


Flashcards

OS ke teen core roles kya hain?
Resource management, hardware abstraction, aur protection (mnemonic: RAP).
OS ko resource management kyun karni padti hai?
Hardware (CPU, RAM, I/O) limited hai aur mutually-untrusting programs ke beech shared hai; OS ise fairly aur efficiently multiplex karta hai.
n processes ke liye CPU utilization formula jab har ek p fraction time wait kare
(idle sirf tab jab saare n simultaneously wait kar rahe hon).
Zyada I/O-bound processes add karne se CPU utilization kyun badhti hai?
Kyunki (probability ki sab wait kar rahe hain) tezi se chhoti hoti hai, toh CPU rarely idle rehta hai.
Hardware abstraction kya hai?
Ek simplified, uniform interface (file, process, socket, virtual memory) jo device-specific details chhupata hai taaki apps kisi bhi hardware par same operations use kare.
OS ke uniform interface ke peeche device-specific details kaun chhupata hai?
Device driver.
Protection enable karne wale do CPU modes kya hain?
User mode (restricted) aur kernel mode (privileged); sirf OS kernel mode mein run karta hai.
Ek user program kernel mein enter karne ka sirf ek legal tarika kya hai?
System call / trap instruction (ek controlled mode switch).
Har access par memory protection kaunsa hardware unit enforce karta hai?
MMU, process ki page table ke against addresses check karke.
Ek user process preemption timer disable kyun nahi kar sakta?
Timer set karna ek privileged instruction hai; user mode mein attempt karna OS ko trap karta hai execute karne ki jagah.
Ek core par true parallelism vs concurrency illusion?
Ek core fast time-slicing karta hai (concurrency illusion); true parallelism ke liye multiple cores chahiye.
Protection policy kaun set karta hai vs mechanism kaun enforce karta hai?
OS policy set karta hai; hardware (mode bit + MMU) mechanism enforce karta hai.

Connections

  • Processes and Threads — woh abstraction jo scheduler manage karta hai.
  • CPU Scheduling — Role 1 ke concrete algorithms.
  • Virtual Memory and Paging — MMU ke zariye RAM ki abstraction + protection.
  • System Calls and Traps — user aur kernel ke beech protected gate.
  • Interrupts and the Timer — woh mechanism jo OS ko CPU wapas lene deta hai.
  • Device Drivers and I/O — jahan hardware abstraction implement hoti hai.
  • Kernel vs User Mode — protection ka hardware basis.

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