4.2.4 · Coding › Operating Systems
Intuition Ek-sentence mein picture
Ek process ek program in execution hai: yeh disk pe pada hua code nahi hai, balki yeh ek live, running activity hai jiske paas apni memory, registers, aur bookkeeping hoti hai. OS har process ko ek PCB (Process Control Block) ke zariye track karta hai aur har process ko kuch states ke beech shuffle karta rehta hai (new, ready, running, blocked, terminated) — kyunki CPU ek waqt mein sach mein sirf ek core pe ek hi process run kar sakta hai.
Tumhare paas ek CPU core hai lekin kaafi saare tasks hain (browser, music, compiler). Tum unhe sach mein ek saath run nahi kar sakte. Toh OS unke beech rapidly switch karta hai — lekin kisi task ko pause karke baad mein bilkul wahi se resume karne ke liye, us task ke baare mein sab kuch kahin save karna padta hai. Woh "sab kuch" PCB hai, aur har task ki current situation uski state hai.
WHAT goes wrong without this? Agar tum kisi task se bina uske CPU registers aur program counter save kiye switch kar lo, toh resume karna aisa hoga jaise coma se uthne ke baad amnesia ho — use pata nahi hoga ki agla instruction kaun sa tha. PCB task ka "memory card" hai.
Ek PCB ek data structure hai jo OS har process ke liye rakhta hai, jisme woh saari information store hoti hai jo us process ko pause aur baad mein resume karne ke liye chahiye. Ek process ke liye ek PCB hota hai, jo usually ek kernel table mein rakha jaata hai.
WHAT's inside? (Isko derive karo yeh poochh ke: "ek process ko perfectly resume karne ke liye mujhe kya yaad rakhna chahiye?")
Mujhe kya yaad rakhna hai
PCB field
WHY
Agla instruction kaun sa hai
Program Counter (PC)
taaki woh sahi line pe resume ho
Working values
CPU registers
calculation ke beech ka data bachna chahiye
Current situation
Process State
ready? blocked? running?
Yeh kaun hai
PID (Process ID)
unique identity
Memory layout
base/limit, page table
taaki uska address space restore ho
Open files, devices
I/O status info
file positions, locks resume karne ke liye
Priority, CPU time used
scheduling info
fair scheduling ke liye
Accounting
CPU usage, limits
billing/limits
Common mistake "PCB program ka code store karta hai."
Yeh sahi kyun lagta hai: ek process hi to program hai, toh surely uska code PCB mein hoga. The fix: code/text process ki memory image (address space) mein rehta hai, PCB mein nahi . PCB metadata + us memory ka pointer store karta hai. PCB chhota hota hai; code bohot bada ho sakta hai.
Definition Paanch classic states
New — process ban raha hai (PCB allocate ho gaya, abhi ready nahi).
Ready — uske paas CPU ke alawa sab kuch hai; ready queue mein wait kar raha hai.
Running — abhi CPU pe execute ho raha hai (ek per core).
Blocked / Waiting — koi event (jaise I/O completion) hone tak aage nahi badh sakta.
Terminated — execution khatam; PCB reclaim hone wala hai.
Har transition ko ek sentence ki tarah padho:
admit: OS decide karta hai ki jagah hai → process ready queue mein join karta hai.
dispatch: scheduler ek ready process choose karta hai aur use CPU deta hai.
timeout/preempt: time-slice khatam ho gaya (ya koi higher-priority process aa gaya) → Ready mein wapas, Blocked mein nahi , kyunki woh run kar sakta hai.
I/O wait: process ne slow data maanga → CPU voluntarily chhodta hai aur wait karta hai.
event done: I/O khatam hua → Ready mein jaata hai (seedha Running mein NAHI — use CPU ke liye phir se queue karna padta hai).
exit: kaam khatam ya kill ho gaya.
Common mistake "Blocked → Running directly jab I/O khatam ho."
Yeh sahi kyun lagta hai: process pehle run kar raha tha, wait khatam ho gaya, toh use resume karna chahiye. The fix: jab event complete hota hai toh process Blocked → Ready jaata hai. CPU kisi aur ke saath busy ho sakta hai; scheduler ko use dobara select karna hoga. Blocked kabhi seedha Running mein nahi jaata.
Common mistake "Timeout ek process ko Blocked bhej deta hai."
Yeh sahi kyun lagta hai: yeh run karna band ho gaya, toh 'waiting' hai. The fix: timeout/preempt pe process abhi bhi bilkul run karne ke kabil hai — bas uski turn gayi — toh woh Ready mein jaata hai. Blocked sirf kisi baahri event ka intezaar karne ke liye hota hai.
Intuition Save aur restore
Process A se B pe switch karna: OS (1) A ke CPU registers + PC ko A ke PCB mein save karta hai, (2) A ki state set karta hai, (3) B ke PCB se B ke registers + PC load karta hai, (4) B = Running set karta hai. Yeh save/restore hi context switch hai . Yeh pure overhead hai (koi useful user work nahi), isliye bahut zyada switching ("thrashing of CPU") buri cheez hai.
Worked example Example 1 — ek file-reading program
Program: ek badi file padho, phir uske numbers ka sum karo.
Launch → New (Why? PCB ban raha hai).
Admitted → Ready (Why? poori tarah set up, CPU ka wait).
Dispatched → Running (Why? scheduler ne ise core diya).
read(file) hit kiya → Blocked (Why? disk slow hai; CPU idle-spin nahi karna chahiye).
Disk ne data diya → Ready (Why? event done, re-queue karna hai — CPU busy ho sakta hai).
Dispatched → Running , sum compute karta hai.
return 0 → Terminated (Why? kaam ho gaya, PCB reclaim hua).
Worked example Example 2 — teen processes, ek core, round-robin (slice = 2 ms)
P1 ko 3 ms CPU chahiye, P2 ko 1 ms phir 5 ms I/O, P3 ko 2 ms.
t=0: P1 Running (P2, P3 Ready).
t=2: P1 timeout → Ready (Why? slice khatam, abhi bhi 1 ms chahiye). P2 Running.
t=3: P2 ne I/O call ki → Blocked (Why? device ka wait). P3 Running.
t=5: P3 khatam → Terminated . P1 Running.
t=6: P1 khatam → Terminated.
t=8: I/O done → P2 Blocked → Ready , baad mein Running.
Key takeaway: preempt hua P1 Ready gaya, lekin I/O-bound P2 Blocked gaya — alag reasons, alag states.
Worked example Example 3 — Why PCB ko PC store karna zaroori hai
Maano P1 instruction 100 pe tha jab timeout hua. Agar hum PC=100 save karna bhool jaate, toh resume pe CPU ka PC jo B ne chhoda woh hoga (maano 540) → P1 B ke code region mein jump kar deta = crash/garbage. Yeh step kyun important hai: PC sabse critical field hai sahi resumption ke liye.
Recall Feynman: 12-saal ke bachche ko samjhao
Socho ek TV hai (CPU) aur kaafi saare bachche hain jo apna-apna show dekhna chahte hain (processes). Ek waqt mein sirf ek bachcha dekhta hai. Jab agla bachche ki baari aati hai, tum ek sticky note pe likh dete ho ki current bachche ka show exactly kahan pause hua tha, unke paas kya snacks the, sab kuch (yeh PCB hai). Phir swap karo. Ek bachcha ho sakta hai: abhi abhi aaya (New), line mein wait kar raha (Ready), abhi dekh raha (Running), bathroom gaya hai aur wapas aane tak nahi dekh sakta (Blocked), ya khatam karke chala gaya (Terminated). Jab bathroom wala bachcha wapas aata hai, woh TV turant nahi uthaa leta — woh line mein wapas aata hai (Ready), kyunki shayad koi aur dekh raha ho!
Mnemonic States aur order yaad karo
"New Recruits Run, But Tire" → N ew, R eady, R unning, B locked, T erminated.
Aur golden rule: "Blocked jaata hai Ready mein, kabhi Running mein nahi." — "Wait karne ke baad, phir se wait karo (line mein)."
Process kya hai? Ek program in execution — woh live activity jiske paas apni memory, registers, aur PCB hoti hai (sirf disk pe pada code nahi).
PCB kya hai? Process Control Block — ek per-process kernel data structure jo process ko pause aur resume karne ke liye zaroori saari info (PC, registers, state, PID, memory map, I/O, scheduling) rakhta hai.
Paanch process states batao. New, Ready, Running, Blocked (Waiting), Terminated.
Kaun si state ka matlab hai "CPU ke alawa sab kuch hai"? Ready.
Kaun si state ka matlab hai "I/O jaisa baahri event ka wait kar raha hai"? Blocked / Waiting.
Jab I/O complete hota hai, ek process Blocked se ___ mein jaata hai (Running nahi). Ready.
Time-slice timeout pe kaun sa transition hota hai? Running → Ready (preempt), Running → Blocked NAHI.
Blocked seedha Running mein kyun nahi jaata? CPU busy ho sakta hai; scheduler ko process ko dobara select karna padta hai, isliye pehle Ready mein aana hota hai.
Sahi resumption ke liye sabse critical PCB field kaun si hai, aur kyun? Program Counter — iske bina CPU ko pata nahi chalega ki agla instruction kaun sa execute karna hai.
Context switch PCBs ke saath kya karta hai? Jaane wale process ke registers/PC uske PCB mein save karta hai, phir aane wale process ke registers/PC uske PCB se load karta hai.
Kya PCB program ka code store karta hai? Nahi — code process ki memory image mein rehta hai; PCB metadata + pointers store karta hai.
Dispatcher/scheduler kya karta hai? Ek Ready process select karta hai aur use CPU deta hai (Ready → Running).
Context Switching — woh mechanism jo PCBs save/restore karta hai.
CPU Scheduling — Ready → Running selection decide karta hai.
Threads vs Processes — threads ek address space share karte hain; processes se lighter hote hain.
Inter-Process Communication — alag processes aapas mein kaise baat karte hain.
Ready Queue and I-O Queues — jahan Ready/Blocked processes physically wait karte hain.
Program vs Process — static code vs dynamic execution.
Interrupts — Running → Ready/Blocked transitions trigger karte hain.
one at a time forces switching
stores PC, registers, state, PID
Process - program in execution
PCB Process Control Block