Introduction (परिचय)
अब तक हमने process के बारे में पढ़ा।
लेकिन कई बार एक ही process के अंदर कई छोटे-छोटे tasks एक साथ चलते हैं।
इन्हीं tasks को threads कहा जाता है।
Example के लिए browser को समझो:
- एक tab open है
- एक file download हो रही है
- एक page scroll हो रहा है
ये काम एक ही process के अंदर अलग-अलग threads की मदद से हो सकते हैं।
Thread क्या होता है
Thread process का सबसे छोटा execution unit होता है।
जब process run करता है, तो उसके अंदर एक या एक से अधिक threads हो सकते हैं।
हर thread अपना अलग execution flow रखता है।
Simple Definition
Thread एक lightweight process है, जो process के अंदर independently execute कर सकता है।
Thread को lightweight process क्यों कहते हैं
Process create करना heavy काम होता है, क्योंकि उसके लिए अलग memory space, resources और control information चाहिए होती है।
लेकिन thread process के resources को share करता है, इसलिए:
- thread create करना आसान होता है
- thread switching fast होती है
- system performance बेहतर होती है
Process aur Thread में अंतर
| Basis | Process | Thread |
|---|---|---|
| Meaning | Running program | Process का execution unit |
| Memory | अलग memory space | Shared memory space |
| Resource | अलग resources | Process ke resources share करता है |
| Creation | Slow | Fast |
| Switching | More time | Less time |
| Communication | मुश्किल | आसान |
Thread Structure
एक process के अंदर कुछ चीजें shared होती हैं और कुछ चीजें हर thread की अपनी होती हैं।
Shared by all threads
- Code section
- Data section
- Open files
- Process resources
Separate for each thread
- Program Counter
- Registers
- Stack
इसका मतलब यह है कि सभी threads एक ही process के data ko use कर सकते हैं, लेकिन हर thread का execution path अलग होता है।
Single Threaded aur Multithreaded Process
Single Threaded Process
इसमें process के अंदर केवल एक ही thread होता है।
एक समय में एक ही काम properly execute होता है।
Example:
कोई simple calculator app
Multithreaded Process
इसमें process के अंदर कई threads होते हैं।
एक ही process एक साथ multiple tasks handle कर सकता है।
Example:
Browser, media player, online editor
Multithreading क्या होता है
जब एक process के अंदर multiple threads होते हैं और वे अलग-अलग tasks perform करते हैं, तो उसे multithreading कहते हैं।
यह technique modern operating systems में बहुत useful है क्योंकि इससे system responsive बनता है।
Types of Threads
1. User-Level Threads
ये threads user space में manage होते हैं।
Operating System directly इन्हें manage नहीं करता।
Features
- Fast creation
- Fast switching
- OS को direct जानकारी नहीं होती
Problem
अगर एक thread block हो जाए, तो बाकी threads भी रुक सकते हैं
2. Kernel-Level Threads
ये threads Operating System द्वारा manage किए जाते हैं।
Features
- Better control
- True parallelism possible
- OS scheduling करता है
Problem
इनका creation और switching थोड़ा slow हो सकता है
Threads के फायदे
1. Responsiveness
Application जल्दी response देता है
Example:
Download चलते हुए भी browser open रहता है
2. Resource Sharing
सभी threads same memory और resources को share करते हैं
3. Economy
Threads बनाना process की तुलना में कम costly होता है
4. Better CPU Utilization
Multiple tasks efficiently run हो सकते हैं
5. Parallelism
Multi-core system में multiple threads parallel run कर सकते हैं
Threads की कमियाँ
1. Synchronization Problem
Shared data होने की वजह से conflict हो सकता है
2. Debugging Difficult
Multithreaded programs को debug करना मुश्किल होता है
3. Security Risk
एक thread की problem पूरे process को affect कर सकती है
4. Deadlock Possibility
अगर threads proper coordination न करें, तो deadlock हो सकता है
Example (समझने के लिए)
मान लो एक browser process है:
- Thread 1 page load कर रहा है
- Thread 2 video play कर रहा है
- Thread 3 file download कर रहा है
- Thread 4 user input handle कर रहा है
यह सभी threads एक ही process के अंदर काम कर रहे हैं।
इसी वजह से browser fast और responsive लगता है।
Real Life Analogy
एक office को process मानो और office के employees को threads।
- Office = Process
- Employees = Threads
सभी employees same office resources use करते हैं, लेकिन हर employee का काम अलग होता है।
इसी तरह threads process के resources share करते हुए अलग-अलग tasks करते हैं।
Exam Point of View
Threads topic में अक्सर ये points पूछे जाते हैं:
- Thread क्या है
- Process aur Thread में अंतर
- Multithreading क्या है
- User-Level aur Kernel-Level Threads
- Threads के advantages
Conclusion
Threads Operating System का बहुत important concept है।
यह एक process के अंदर multiple execution units provide करते हैं, जिससे system fast, efficient और responsive बनता है।
Modern applications में multithreading का use बहुत common है, क्योंकि इससे एक ही application कई काम एक साथ handle कर सकती है।