Thread क्या है? OS में Types, Lifecycle और TCB की पूरी जानकारी
Thread क्या है? — Process की Smallest Execution Unit
Thread किसी Process की smallest unit of CPU execution होती है — यानी CPU actually किसी process को नहीं, बल्कि उसके अंदर की thread को execute करता है।
एक process के अंदर एक या एक से अधिक threads हो सकती हैं। हर thread अपना अलग काम करती है, लेकिन सभी threads मिलकर process के resources share करती हैं — जैसे memory, files, और code।
Thread is the smallest unit of execution inside a Process.
Multithreading in Operating System आज के हर modern software का आधार है — Chrome से लेकर Android apps तक, सब threads पर ही चलते हैं।
Thread को आसान भाषा में समझें — Restaurant Example
एक Restaurant की कल्पना करें:
- Restaurant = Process
- Waiters = Threads
सभी waiters एक ही kitchen use करते हैं, एक ही restaurant के resources लेते हैं — लेकिन अलग-अलग customers को serve करते हैं। ठीक इसी तरह, एक process के अंदर कई threads एक साथ काम करती हैं, resources share करते हुए।
Thread की विशेषताएँ
- Smallest unit of execution होती है
- CPU द्वारा directly execute होती है
- Lightweight और fast होती है
- Process के resources share करती है
- नई process बनाने से कहीं सस्ती होती है
Thread के Components — क्या अपना, क्या shared?
हर thread के पास अपनी अलग जानकारी होती है:
| Component | काम |
|---|---|
| Thread ID (TID) | Thread की unique पहचान |
| Program Counter (PC) | अगली execute होने वाली instruction |
| Register Set | Calculation के values |
| Stack | Function calls और local variables |
| Thread State | Running / Blocked / Ready |
और यह सब shared होता है — सभी threads मिलकर use करती हैं:
- Code Segment
- Data Segment
- Heap Memory
- Open Files
- Signals
Process
+---------------------------+
| Code Segment |
| Data Segment |
| Heap Memory |
| Shared Files |
+---------------------------+
│ │ │
▼ ▼ ▼
Thread-1 Thread-2 Thread-3
Stack Stack Stack
Registers Registers Registers
PC PC PC
Single-Threaded vs Multithreaded Process
Single-Threaded Process में केवल एक thread होती है — एक काम, एक रास्ता।
उदाहरण: Calculator, Notepad, simple command-line programs
Process → Thread → CPU
Multithreaded Process में एक से ज़्यादा threads होती हैं — एक साथ कई काम।
उदाहरण: Google Chrome, MS Word, Visual Studio, Android Apps
Process
├── Thread-1
├── Thread-2
├── Thread-3
└── Thread-4
└── CPU
Chrome में एक tab crash हो जाए तो दूसरे tabs चलते रहते हैं — यही multithreading की ताकत है।
Thread क्यों ज़रूरी है?
अगर सिर्फ processes use की जाएं:
- Memory ज़्यादा लगेगी
- Context Switching slow होगी
- Process creation में time waste होगा
Thread इन तीनों समस्याओं को एक साथ solve करती है।
Benefits of Thread — 7 मुख्य फायदे
1. Faster Execution
Thread बहुत तेजी से execute होती है — process create करने का overhead नहीं होता।
2. Better CPU Utilization
एक thread I/O wait में हो तो दूसरी thread CPU use कर सकती है — CPU idle नहीं रहता।
3. Resource Sharing
सभी threads एक ही code, data, files और memory share करती हैं — अलग से allocate करने की ज़रूरत नहीं।
4. Easy Communication
Threads एक ही memory space share करती हैं, इसलिए आपस में communicate करना बेहद fast होता है। Processes को IPC (Inter-Process Communication) की ज़रूरत पड़ती है — threads को नहीं।
5. Less Memory
नई process बनाने की तुलना में thread बहुत कम memory लेती है।
6. Fast Context Switching
Thread switching, process switching से कहीं तेज़ होती है — milliseconds से भी कम।
7. Parallel Execution
Modern multi-core CPUs में अलग-अलग threads अलग-अलग cores पर एक साथ execute हो सकती हैं — यह real parallelism है।
Advantages और Disadvantages
Advantages ✅
| फायदा | क्यों? |
|---|---|
| Fast Execution | Process overhead नहीं |
| Less Memory | Shared resources |
| Better Responsiveness | UI hang नहीं करता |
| Fast Context Switching | Lightweight होती है |
| Higher Throughput | ज़्यादा काम, कम time |
| Low Overhead | Cheap to create |
Disadvantages ❌
| नुकसान | क्यों होता है? |
|---|---|
| Synchronization Problem | Shared memory पर conflict |
| Race Condition | एक साथ access करने से data corrupt |
| Deadlock | Threads एक-दूसरे का wait करती रहें |
| Difficult Debugging | Multi-thread bugs reproduce करना मुश्किल |
| Shared Memory Errors | एक thread की गलती सबको affect करती है |
Thread Life Cycle — 5 States
हर thread इन states से गुज़रती है:
New → Runnable → Running → Blocked → Runnable → Running → Terminated
| State | मतलब |
|---|---|
| New | Thread create हुई, अभी start नहीं हुई |
| Runnable | Ready है, CPU का इंतज़ार है |
| Running | CPU मिला, execute हो रही है |
| Blocked | I/O या किसी event का wait कर रही है |
| Terminated | Execution complete, thread खत्म |
Blocked state के बाद thread directly Runnable में जाती है — Running में नहीं। यह exam में common mistake है।
Process vs Thread — सबसे Important Comparison
| Process | Thread |
|---|---|
| Heavyweight | Lightweight |
| Independent unit | Process का हिस्सा |
| अलग memory space | Shared memory |
| Slow creation | Fast creation |
| Slow context switching | Fast context switching |
| Communication slow (IPC चाहिए) | Communication fast (shared memory) |
| PCB होता है | TCB होता है |
| Expensive | Cheap |
एक line में: Process एक घर है, Thread उस घर में रहने वाले लोग — सब मिलकर resources share करते हैं।
Thread Control Block (TCB)
जैसे process के लिए PCB होता है, thread के लिए Thread Control Block (TCB) होता है। OS हर thread की information यहाँ store करता है:
- Thread ID (TID)
- Registers
- Program Counter
- Stack Pointer
- Thread State
- Scheduling Information
Types of Thread — User Level vs Kernel Level
OS में मुख्यतः दो प्रकार की threads होती हैं:
Thread
├── User Level Thread (ULT)
└── Kernel Level Thread (KLT)
User Level Thread (ULT)
ULT पूरी तरह user space में manage होती हैं — kernel को इनके बारे में पता तक नहीं होता। एक thread library (जैसे POSIX pthreads) इन्हें handle करती है।
Application → Thread Library → User Threads → Kernel → CPU
| ULT के फायदे ✅ | ULT के नुकसान ❌ |
|---|---|
| Fast creation | एक block → पूरी process block |
| Fast switching | Multi-core का पूरा फायदा नहीं |
| Low cost | Limited parallelism |
| No kernel trap | — |
Kernel Level Thread (KLT)
KLT सीधे OS kernel द्वारा manage की जाती हैं। Kernel हर thread को जानता है और independently schedule कर सकता है।
Application → Kernel → Kernel Threads → CPU
| KLT के फायदे ✅ | KLT के नुकसान ❌ |
|---|---|
| True parallelism | Context switching महंगी |
| Multi-core support | Kernel mode switching slow |
| एक block → बाकी चलती रहें | Complex implementation |
| Better scheduling | More overhead |
User Level Thread vs Kernel Level Thread
| Feature | User Level Thread | Kernel Level Thread |
|---|---|---|
| Management | User library | Kernel |
| Speed | Fast | Slow |
| Cost | Cheap | Costly |
| Kernel Awareness | नहीं | हाँ |
| Parallelism | Limited | True |
| Implementation | Easy | Complex |
Thread Models — ULT और KLT की Mapping
User threads और kernel threads के बीच जो relationship होती है उसे Thread Model कहते हैं। तीन main models हैं:
1. Many-to-One Model
कई user threads → एक kernel thread
UT1 ┐
UT2 ├──→ KT1
UT3 ┘
- फायदा: Fast
- नुकसान: एक block हुई → सभी block; multi-core का फायदा नहीं
2. One-to-One Model
हर user thread → एक अलग kernel thread
UT1 → KT1
UT2 → KT2
UT3 → KT3
- फायदा: True parallelism, multi-core support
- नुकसान: ज़्यादा kernel threads, ज़्यादा memory
3. Many-to-Many Model
कई user threads → कई kernel threads (flexible mapping)
UT1 ┐ ┌ KT1
UT2 ├──→────┤
UT3 ┘ └ KT2
- फायदा: Best performance, flexible, better resource utilization
- नुकसान: Design complex है
Thread Models Comparison
| Model | Mapping | Parallelism | Performance |
|---|---|---|---|
| Many-to-One | Many ULT → One KLT | ❌ | Medium |
| One-to-One | One ULT → One KLT | ✅ | High |
| Many-to-Many | Many ULT → Many KLT | ✅✅ | Best |
FAQ — Thread के बारे में Common Questions
Q1. Thread और Process में सबसे बड़ा फर्क क्या है?
सबसे बड़ा फर्क memory का है। हर process का अपना अलग memory space होता है — एक process दूसरे की memory directly access नहीं कर सकती। लेकिन एक process की सभी threads एक ही memory share करती हैं। इसीलिए thread creation fast है, communication fast है, और context switching भी कम expensive है।
Q2. Multithreading से Deadlock क्यों होता है?
Deadlock तब होता है जब दो या ज़्यादा threads एक-दूसरे का resource release होने का इंतज़ार करती रहें — और कोई भी आगे नहीं बढ़ पाए। यह shared memory का एक common problem है। इसे Mutex, Semaphore जैसे synchronization tools से handle किया जाता है।
Q3. कौन सा Thread Model सबसे अच्छा है?
Practically Many-to-Many model सबसे flexible और efficient है — यह true parallelism देता है और system को unnecessarily ज़्यादा kernel threads भी नहीं बनाने पड़ते। लेकिन implement करना complex है। Linux जैसे modern OS One-to-One model use करते हैं क्योंकि hardware fast हो गया है और overhead acceptable है।
💬 Leave a Comment & Rating