Lesson 2 � Beginner
OS: Processes vs Threads
Process ek running program hai, Thread uska lightweight version hai. Ye concept OS ki sabse fundamental cheez hai � interview mein har baar poochte hain.
Process hota kya hai?
WHAT
Process ek program ka execution instance hai. Jab tum Chrome kholte ho toh ek process start hota hai. Iske apne memory space, registers, program counter hota hai � ek independent unit hai.
WHEN
Jab bhi tum koi application run karte ho � ek process create hota hai. OS process ko manage karta hai � scheduling, memory allocation, I/O handling.
WHERE
Chrome (har tab ek process), VS Code (editor process), Python script run karna � sab processes hain. OS mein Process Control Block (PCB) hota hai jo process info store karta hai.
# Process ke Components:
- Code Segment: Program ka code
- Data Segment: Global variables
- Heap: Dynamic memory allocation
- Stack: Function calls, local variables
- Registers: CPU registers ka snapshot
- PC (Program Counter): Next instruction address
# Process States (5 States):
NEW ✓ READY ✓ RUNNING ✓ TERMINATED
? ?
WAITING (I/O)
1. NEW: Process create ho raha hai
2. READY: CPU ke liye wait kar raha hai
3. RUNNING: CPU pe execute ho raha hai
4. WAITING: I/O operation ka wait
5. TERMINATED: Execution complete
Thread hota kya hai?
# Thread = Lightweight Process
✓ Process ke andar ka execution unit
✓ Same process ke threads same memory share karte hain
✓ har thread ka apna PC, stack, registers hota hai
# Example: Chrome Browser
Process: Chrome Browser
Thread 1: Tab 1 (website load)
Thread 2: Tab 2 (video play)
Thread 3: Download manager
Thread 4: Renderer
# Thread Types:
1. User Thread: User space mein managed (by library)
2. Kernel Thread: OS kernel dwara managed
3. Green Thread: Language runtime managed (Go goroutines)
# Thread ka Fayda:
✓ Parallel execution within a process
? (shared memory) � fast communication
✓ Context switch fast hai (sirf stack change)
✓ Lightweight � create/delete fast
Process vs Thread � Key Differences
| Feature | Process | Thread |
|----------------|----------------------------|-----------------------------|
| Memory | Separate (isolated) | Shared (within process) |
| Creation Time | Slow (heavy) | Fast (lightweight) |
| Context Switch | Slow (sab change hota hai) | Fast (sirf stack change) |
| Communication | IPC (Inter-Process Comm) | Direct (shared memory) |
| Failure Impact | Sirf ek process crash | Poori process crash ho sakti|
| Resource | Zyada (apna memory space) | Kam (shared resources) |
| Example | Chrome, VS Code | Tab threads, worker threads |
# IPC Methods (Process Communication):
1. Pipes � one-way communication
2. Message Queues � structured messages
3. Shared Memory � fastest but complex
4. Sockets � network communication
5. Signals � async notification
Context Switching
# Context Switch: CPU ek process se doosre pe switch karna
# Process Context Switch:
1. Current process ka state save karo (PCB mein)
2. New process ka state load karo (PCB se)
3. PC update karo � naya instruction start
4. Registers load karo
✓ Time: 1-100 microseconds
✓ EXPENSIVE!
# Thread Context Switch (same process):
1. Sirf stack pointer change karo
2. Registers change karo
✓ Time: < 1 microsecond
✓ CHEAP!
# Why Context Switch is slow:
✓ Cache flush hota hai
✓ TLB (Translation Lookaside Buffer) flush
✓ Pipeline flush
✓ OS overhead
# Interview Question:
"Context switching mein kya hota hai?"
Answer: Process ka state PCB mein save hota hai,
naya process load hota hai, CPU registers update
hote hain. Cache aur TLB flush hota hai.
Exercise
Question: Process aur Thread ka sabse fundamental difference kya hai memory ke context mein? (2 words)
Question: Context switching mein process ka state kahan save hota hai? (3 words ya acronym)
Common mistakes
- Thread = Process sochna: Thread process ke andar hota hai. Process independent hai, thread dependent.
- Context switching free sochna: Context switching expensive hai � performance impact hota hai. Threads zyada efficient hain same process mein.
- IPC ignore karna: Process communication IPC se hota hai � pipes, sockets, shared memory. Ye interview mein poochte hain.
- Thread safety bhoolna: Shared memory mein race conditions hoti hain � mutex/semaphore se protect karo.
Processes vs Threads samajh aa gaya✓ Ab Memory Management seekhte hain � virtual memory, paging, segmentation.