BCA / B.Tech 15 min read

Threads in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Threads in Hindi - General Topics
  • • अवलोकन: Quick Answer: Operating system में Thread किसी process के execution की सबसे छोटी unit होती है। इसे lightweight process भी कहा जाता है। एक ही process के...
  • • मुख्य अवधारणाएं: Threads, User kernel level multithreading Hindi, Thread models benefits,Threads, User kernel level multithreading, Thread models benefits
Quick Answer: Operating system में Thread किसी process के execution की सबसे छोटी unit होती है। इसे lightweight process भी कहा जाता है। एक ही process के अंदर कई threads एक साथ (concurrently) चल सकते हैं, जो process के code, data और open files को आपस में share करते हैं, लेकिन हर thread का अपना अलग Thread ID, Program Counter (PC), Register set और Stack होता है।

Thread in OS क्या है? (Threads in Operating System in Hindi)

आम बोलचाल में thread का मतलब "धागा" या किसी काम की एक लगातार चलने वाली लड़ी होता है। Operating system में जब कोई program run होता है, तो OS उसे एक process बनाता है। पहले के systems में एक process के अंदर सिर्फ एक ही काम एक समय पर हो पाता था, जिसे single-threaded process कहते थे। लेकिन modern operating systems में एक ही process के अंदर कई अलग-अलग tasks एक साथ चलाए जा सकते हैं। इन्हीं छोटे-छोटे execution units को Thread कहा जाता है।

इसे एक आसान real-life example से समझें: मान लीजिए एक software company एक process है। उस company का office, computers और files shared resources हैं। Company में काम करने वाले अलग-अलग employees threads हैं। सभी employees एक ही office और data share करते हैं, लेकिन हर employee का अपना काम, अपनी notebook (stack) और सोचने की speed (registers) अलग होती है। सब मिलकर एक साथ काम करते हैं ताकि company का काम तेज़ी से पूरा हो सके।

OS में thread को CPU utilization की basic unit माना जाता है। CPU scheduler वास्तव में processes को नहीं, बल्कि threads को CPU time allocate करता है।

Single-Threaded Process              Multi-Threaded Process
+--------------------------------+   +--------------------------------+
|  Code   |   Data   |   Files   |   |  Code   |   Data   |   Files   |
+--------------------------------+   +--------------------------------+
| Registers     | Stack          |   | Reg | Reg | Reg | Stack| Stack |
| (Thread 1)    | (Thread 1)     |   | (T1)| (T2)| (T3)| (T1) | (T2)  |
+--------------------------------+   +--------------------------------+
Advertisement

Thread की Structure: कौन से Resources Share होते हैं?

Exam में अक्सर पूछा जाता है कि एक process के कई threads आपस में क्या share करते हैं और हर thread के पास अपना क्या निजी (private) होता है। इसे समझना बहुत ज़रूरी है:

1. Threads आपस में क्या Share करते हैं?

  • Code Section: Program का compiled machine code सभी threads के लिए common होता है।
  • Data Section: Global variables और static variables सभी threads share करते हैं। अगर एक thread किसी global variable की value बदलता है, तो दूसरे thread को भी बदली हुई value दिखाई देती है।
  • OS Resources: Process की open files, network sockets, child processes और signals सभी threads के बीच shared होते हैं।
  • Heap Memory: Runtime पर allocate होने वाली dynamic memory भी सभी threads share करते हैं।

2. हर Thread का अपना Private (Individual) क्या होता है?

  • Thread ID (TID): हर thread को पहचान के लिए एक unique number मिलता है।
  • Program Counter (PC): यह track रखता है कि उस thread का अगला कौन सा instruction execute होना है। अलग-अलग threads code के अलग-अलग हिस्से run कर सकते हैं।
  • Register Set: Thread के current calculation values और CPU registers का set उसका अपना होता है।
  • Stack: Local variables, function parameters और return addresses store करने के लिए हर thread का अपना अलग stack होता है।
Resource का प्रकार Resource का नाम Shared या Thread-Private? कारण
Memory Code Section Shared सभी threads एक ही program का हिस्सा हैं
Memory Data Section (Global Variables) Shared Threads के बीच data sharing आसान और fast हो
Memory Heap Shared Dynamic memory allocation process level पर होता है
OS Resources Open Files / Sockets Shared File descriptors process level पर maintain होते हैं
Execution State Thread ID Thread-Private OS हर thread को अलग से identify कर सके
Execution State Program Counter (PC) Thread-Private हर thread अलग line of code execute कर सकता है
CPU State Register Set Thread-Private हर thread की temporary calculation अलग होती है
Memory Stack Thread-Private Local variables और function calls स्वतंत्र रहें

Types of Threads in Operating System

Operating system में implementation के आधार पर threads दो मुख्य categories में बांटे जाते हैं:

1. User-Level Threads (ULT)

User-level threads user space में run होते हैं। इनका सारा management (thread creation, scheduling, synchronization) user space में मौजूद thread library (जैसे POSIX Pthreads, Java threads) से होता है।

  • OS kernel को इन threads के बारे में कोई जानकारी नहीं होती। Kernel पूरे process को सिर्फ एक single entity मानता है।
  • Thread create करना और switch करना बहुत तेज़ होता है, क्योंकि system call करने या kernel mode में जाने की ज़रूरत नहीं पड़ती।
  • कमी: अगर एक user thread कोई blocking system call (जैसे I/O wait) कर दे, तो kernel पूरे process को block कर देता है, जिससे बाकी threads भी रुक जाते हैं। इसके अलावा multi-core processor होने पर भी kernel इन threads को अलग-अलग cores पर नहीं बांट पाता।

2. Kernel-Level Threads (KLT)

Kernel-level threads सीधे operating system के kernel से manage और schedule होते हैं। Modern OS (जैसे Windows, Linux, macOS) इसी model पर काम करते हैं।

  • Kernel हर thread का context information (TCB - Thread Control Block) खुद maintain करता है।
  • अगर एक thread blocking call में चला जाए, तो kernel उसी process के दूसरे thread को schedule करके CPU पर चला सकता है।
  • Multi-core CPU पर kernel अलग-अलग threads को अलग-अलग cores पर parallel execute कर सकता है।
  • कमी: Thread create करने और context switch करने में kernel mode में transition करना पड़ता है, इसलिए यह user thread से धीमा होता है।

User-Level Threads vs Kernel-Level Threads

Point User-Level Threads (ULT) Kernel-Level Threads (KLT)
Management User-level thread library से manage होते हैं Operating system का kernel manage करता है
OS Kernel को जानकारी Kernel को इन threads का पता नहीं होता Kernel हर thread को पहचानता है
Context Switch Speed बहुत तेज़ (No mode switch) धीमा (User से kernel mode switch होता है)
Blocking System Call एक thread block हुआ तो पूरा process block एक thread block होने पर दूसरा thread चल सकता है
Multiprocessor Support Multi-core CPU का पूरा फायदा नहीं उठा पाते Threads को अलग-अलग cores पर parallel चलाया जा सकता है
Implementation Simple होता है Complex होता है
Examples Java Green Threads, GNU Portable Threads Windows threads, Linux Pthreads (NPTL)

Multithreading Models in OS

User-level threads को kernel-level threads से connect करने के लिए operating system तीन मुख्य models इस्तेमाल करता है:

  1. Many-to-One Model: कई user-level threads को सिर्फ एक kernel thread पर map किया जाता है। Thread management तेज़ होता है, लेकिन एक thread block हुआ तो सब रुक जाते हैं और multi-core CPU का फायदा नहीं मिलता।
  2. One-to-One Model: हर एक user thread के लिए एक dedicated kernel thread बनाया जाता है। इसमें high concurrency मिलती है और multi-core पर true parallelism मिलता है। Linux और Windows इसी model का इस्तेमाल करते हैं। इसका नुकसान सिर्फ यह है कि बहुत ज़्यादा threads बनाने से system पर overhead बढ़ जाता है।
  3. Many-to-Many Model: कई user threads को बराबर या कम संख्या के kernel threads पर multiplex किया जाता है। यह दोनों models का best compromise है, लेकिन इसे implement करना काफी complex होता है।

Process और Thread में अंतर (Difference Between Process and Thread)

BCA और B.Tech exams में Process vs Thread सबसे ज़्यादा पूछा जाने वाला question है। नीचे दिए गए comparison table को अच्छे से याद रखें:

Comparison Point Process (Heavyweight) Thread (Lightweight)
Definition Execution में चल रहा पूरा program process कहलाता है। Process के अंदर execution का सबसे छोटा segment thread है।
Memory और Resources हर process का अपना अलग address space और memory होती है। एक process के सभी threads shared address space का इस्तेमाल करते हैं।
Creation Time Process create करना धीमा होता है और ज़्यादा resources लगते हैं। Thread create करना बहुत तेज़ होता है और कम resources लगते हैं।
Context Switching Process context switch में CPU cache flush और page tables बदलने होते हैं, जो धीमा है। Thread context switch में केवल registers और stack बदलना होता है, जो बहुत तेज़ है।
Communication (IPC) अलग processes के बीच data exchange के लिए heavy OS mechanism चाहिए। detail में देखें: Inter Process Communication in Hindi. Threads shared memory से सीधे data read/write कर सकते हैं, इसलिए communication तेज़ है।
Independence (Isolation) Processes एक-दूसरे से पूरी तरह isolated होते हैं। एक crash हो तो दूसरा चलता रहता है। Threads isolated नहीं होते। एक thread का illegal memory access पूरे process को crash कर सकता है।
Termination Process को terminate करने में ज़्यादा समय लगता है। Thread को terminate करना आसान और quick होता है।

Real-Life Worked Example: Multithreading कैसे काम करता है?

मान लीजिए आप अपने computer पर Word Processor (जैसे MS Word) में एक assignment लिख रहे हैं। अगर Word Processor single-threaded होता, तो जब वह file को disk पर auto-save कर रहा होता, उस 1-2 second के लिए आपकी typing रुक जाती और screen freeze हो जाती।

लेकिन modern software में Multithreading का उपयोग होता है:

  • Thread 1 (UI Thread): आपके keyboard strokes और mouse clicks को सुनता है और screen पर text तुरंत type करता है।
  • Thread 2 (Spell Checker Thread): Background में आपके लिखे शब्दों की spelling और grammar check करके लाल underline दिखाता है।
  • Thread 3 (Auto-Save Thread): हर 2 मिनट बाद document को चुपचाप hard disk पर save करता रहता है।

Thread Execution Trace

मान लीजिए CPU time slices (quantum) में काम कर रहा है। नीचे दिया गया trace दिखाता है कि एक single core CPU पर तीनों threads कैसे बारी-बारी से run होकर user को seamless experience देते हैं:

Time Slot Active Thread क्या Action हुआ User Experience
0 ms – 10 ms Thread 1 (UI) User ने letter 'A' type किया, screen पर render हुआ बिना किसी lag के typing चालू है
10 ms – 20 ms Thread 2 (Grammar) पिछले sentence की spelling dictionary से verify की Background में check हो गया
20 ms – 30 ms Thread 1 (UI) User ने letter 'B' type किया, screen update हुई Smooth input response
30 ms – 40 ms Thread 3 (Auto-Save) Document के 500 characters file में write किए User को पता भी नहीं चला और data save हो गया
40 ms – 50 ms Thread 1 (UI) User ने mouse scroll किया, page नीचे हुआ No freezing, perfect responsiveness
Advertisement

C Language में POSIX Threads (Pthreads) का Program

नीचे दिया गया C program दो threads create करता है, जो concurrently चलते हैं और अपना-अपना task पूरा करते हैं:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

// Thread 1 के लिए function
void* worker_one(void* arg) {
    for (int i = 1; i <= 3; i++) {
        printf("Thread 1 execution count: %d\n", i);
        sleep(1);
    }
    return NULL;
}

// Thread 2 के लिए function
void* worker_two(void* arg) {
    for (int i = 1; i <= 3; i++) {
        printf("Thread 2 execution count: %d\n", i);
        sleep(1);
    }
    return NULL;
}

int main() {
    pthread_t t1, t2;

    // दो threads create किए गए
    pthread_create(&t1, NULL, worker_one, NULL);
    pthread_create(&t2, NULL, worker_two, NULL);

    // Main thread दोनों के खत्म होने का wait करता है
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    printf("Dono threads complete ho gaye. Program finished.\n");
    return 0;
}

Program का Real Output:

Thread 1 execution count: 1
Thread 2 execution count: 1
Thread 1 execution count: 2
Thread 2 execution count: 2
Thread 1 execution count: 3
Thread 2 execution count: 3
Dono threads complete ho gaye. Program finished.

ध्यान दें: ऊपर दिया गया output एक sample है; हर बार run करने पर Thread 1 और Thread 2 की lines का order बदल सकता है, क्योंकि कौन सा thread पहले चलेगा यह OS scheduler तय करता है।

Multithreading के Advantages और Limitations

Advantages of Multithreading

  1. Responsiveness: अगर program का कोई एक हिस्सा लंबा calculation कर रहा है या I/O का wait कर रहा है, तब भी user interface वाला thread चलता रहता है और program responsive बना रहता है।
  2. Resource Sharing: Threads अपने process की memory और files share करते हैं, इसलिए उन्हें आपस में data share करने के लिए shared memory setup या message queues जैसे भारी mechanisms की ज़रूरत नहीं होती।
  3. Economy (कम लागत): नया process बनाने के लिए OS को नया address space और page table allocate करना पड़ता है। Thread बनाने में सिर्फ नया stack और registers लगते हैं, जिससे CPU time और memory दोनों बचते हैं।
  4. Multi-core Utilization (Scalability): Modern computers में 4, 8 या 16 cores होते हैं। Multithreading के ज़रिए अलग-अलग threads को अलग-अलग cores पर parallelly चलाया जा सकता है, जिससे execution speed कई गुना बढ़ जाती है।

Limitations और Challenges

  1. Synchronization की ज़रूरत: जब कई threads एक ही global variable या data को एक साथ बदलते हैं, तो race condition पैदा हो जाती है। इसे रोकने के लिए mutex या semaphore जैसे locks लगाने पड़ते हैं। critical section के rules detail में पढ़ें: Critical Region in Hindi.
  2. Deadlock का जोखिम: अगर दो threads दो अलग resources को lock करके एक-दूसरे के resource का इंतज़ार करने लगें, तो deadlock हो सकता है। समझें: Deadlock Characterization.
  3. Debugging और Testing कठिन: Threads का execution order unpredictable होता है। एक बार code सही चल सकता है और दूसरी बार timing की वजह से crash हो सकता है। ऐसे bugs को पकड़ना बहुत मुश्किल होता है।
  4. No Isolation: किसी एक thread की memory leak या pointer error पूरे process को crash कर सकती है।

Students की आम गलतियाँ (Common Mistakes)

  • गलती 1: Concurrency और Parallelism को एक ही समझना। Concurrency का मतलब है कई tasks पर एक साथ progress होना (जैसे single core CPU पर fast context switching)। Parallelism का मतलब है सचमुच एक ही समय (exact same time) पर कई tasks का अलग-अलग CPU cores पर चलना। Single core CPU concurrent हो सकता है, parallel नहीं।
  • गलती 2: यह सोचना कि thread का अपना code या data section होता है। Code और global data हमेशा पूरे process का shared हिस्सा होता है। Thread के पास सिर्फ अपना private stack, registers, PC और TID होता है।
  • गलती 3: Thread context switch में memory flush मानना। Process switch में address space बदलता है, इसलिए TLB/Cache flush होती है। Thread switch में address space वही रहता है, इसलिए memory cache flush नहीं होती।
  • गलती 4: ज़्यादा threads मतलब हमेशा ज़्यादा speed मानना। अगर आप 4-core CPU पर 1000 threads बना देंगे, तो CPU सिर्फ context switching में उलझा रहेगा और speed बढ़ने के बजाय कम हो जाएगी (इसे thread thrashing कहते हैं)।

Exam में कैसे लिखें (5 Marks Ready Answer)

अगर exam में 5 marks का question आए "What is Thread? Explain its advantages." तो sheet में इस format में लिखें:

  1. Definition (2 lines): Thread किसी process के execution की सबसे छोटी unit है। इसे lightweight process भी कहते हैं। यह process के resources share करता है।
  2. Components (Private vs Shared points):
    • Shared: Code, Data, Heap, Open Files.
    • Private: Thread ID, Program Counter, Registers, Stack.
  3. Small Diagram: Single-threaded vs Multi-threaded process का block diagram (Code, Data, Files box ऊपर और नीचे stacks/registers).
  4. Four Key Advantages:
    • Responsiveness: Program freeze नहीं होता।
    • Resource Sharing: Memory duplication नहीं होती।
    • Economy: Creation और switching बहुत fast।
    • Scalability: Multi-core architecture का पूरा उपयोग।
  5. Example: Word Processor में typing, grammar check और auto-save का अलग-अलग threads पर चलना।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Thread क्या है? इसे lightweight process क्यों कहते हैं? (क्योंकि इसके creation और switching में process से बहुत कम overhead और memory लगती है)
  2. एक process के दो threads आपस में कौन से दो resources share करते हैं? (Code section और Data section/Global variables)
  3. Thread का Program Counter (PC) क्या काम करता है? (यह track रखता है कि उस specific thread का अगला instruction कौन सा execute होना है)
  4. User-level thread और Kernel-level thread में कोई एक मुख्य अंतर बताइए। (User thread library से manage होता है, kernel thread OS kernel से manage होता है)
  5. One-to-One multithreading model का सबसे बड़ा advantage क्या है? (Multi-core systems पर true parallel execution मिलता है)

Long Questions (5–10 marks)

  1. Thread in Operating System क्या है? Single-threaded और Multi-threaded process के बीच diagram के साथ अंतर स्पष्ट कीजिए।
  2. Process और Thread में अंतर लिखिए। Operating system में multithreading के मुख्य फायदे और चुनौतियां क्या हैं?
  3. User Level Threads और Kernel Level Threads की तुलना कीजिए। Multithreading के तीनों models (Many-to-One, One-to-One, Many-to-Many) को detail में समझाइए।

FAQs

Thread in OS in simple words क्या होता है?

आसान शब्दों में, thread किसी बड़े program या process का एक छोटा सा स्वतंत्र worker है। एक process के अंदर कई workers (threads) एक साथ काम करके program को fast और responsive बनाते हैं।

Thread को lightweight process क्यों कहते हैं?

क्योंकि नया thread बनाने के लिए OS को नई memory या नया address space नहीं बनाना पड़ता। Thread पुराने process की memory ही इस्तेमाल करता है, इसलिए यह बहुत कम CPU time और RAM लेता है।

User level thread और kernel level thread में कौन सा fast होता है?

Creation और switching के मामले में User-level thread ज़्यादा fast होता है, क्योंकि इसमें OS kernel को interrupt नहीं करना पड़ता और कोई system call overhead नहीं होता।

अगर एक thread crash हो जाए तो क्या पूरा process बंद हो जाता है?

हाँ, अधिकांश operating systems में अगर कोई thread segmentation fault या illegal memory access करता है, तो OS पूरे process को terminate कर देता है, जिससे उसके बाकी सभी threads भी बंद हो जाते हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Operating system में Thread किसी process के execution की सबसे छोटी unit होती है।
Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स के पाठ्यक्रम में Threads in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Threads in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Threads in Hindi
Distributed System in Hindi
Real Time System in Hindi
System Calls in Hindi
System Program in Hindi
Structure of Operating System in Hindi
Layers Design of Structure in Hindi
Unix in Hindi
Virtual Machine in Hindi
Kernel-Based Operating System in Hindi
Process in Hindi – OS में Process क्या है
Interacting Processes in Hindi
Fundamental of Schedulling in Hindi
Scheduling Criteria in Hindi
Long Medium Short Term Scheduling in Hindi
Scheduling Algorithms in Hindi
Structure of Concurrent System in Hindi
Critical Region in Hindi
Critical Section in Hindi
Inter process Communication in Hindi
Monitor in Hindi
Semaphore in OS in Hindi – Counting और Binary Semaphore
Semaphores Implementation & Uses in Hindi
Logical and Physical Address in Hindi
Swapping in Hindi
Contiguous Allocation in Hindi
Segmentation in Hindi
Paging in OS in Hindi – Paging और Segmentation
Paging with Segmentation in Hindi
kernel Memory Allocation in Hindi
Page Replacement Algorithms in Hindi
Virtual Memory in Hindi
Virtual Memory with demand Paging in Hindi
Deadlock & Characterization in Hindi
Methods for Handling Deadlocks in Hindi
Deadlock Thrashing in Hindi
Allocation of Frames in Hindi
Virtual Memory using Segmentation in Hindi
Interprocess Communication protocol in Hindi
Network Operating System in Hindi
Design Issues in Distributed File System in Hindi
Network Structure in Hindi
Structure Distributed System in Hindi
File System and Coordination in Hindi
Linux in Hindi
Commands of Linux in Hindi | लिनक्स के कमांड्स हिंदी में
Programmer Interface & File manipulation in Hindi
Process control in Linux in Hindi
Linux Kernel & Signals in Hindi
File System in Linux in Hindi
block & inodes in Hindi
System Editor in Hindi
Character Transliteration in Hindi
ED in Hindi
Vi Editor & There Commands in Hindi
Shell Script in Hindi
Looping & Decision Making in Hindi
Variables & File name Expansion in Hindi
Array in Hindi OS
Sub Program in Hindi
C interface with Linux in Hindi
Simpel Shell Programs in Hindi
BCA Part-1 | Operating Systems | Semester-I | MDSU Exam Paper 2023 (Held in 2024)
What is Operating System (OS) in Hindi
Operating System (OS) All Important Questions and Answers in Hindi (MDSU)
BCA || Operating System 2025 Paper || MDSU Exam Paper
BCA | OS(Operating System) 2023 Paper | MDSU Exam Paper
Type of Operating System in Hindi
Goals of Operating System in Hindi
Operation of Operating system in Hindi
Resource Allocation & Functions in Hindi
class of Operating system in Hindi
Batch Processing in Hindi
Multi Processing in Hindi
Time Sharing in Hindi