BCA / B.Tech 12 min read

Semaphore in OS in Hindi – Counting और Binary Semaphore

RK

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

प्रकाशित:

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

  • • विषय फोकस: Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Semaphore in OS in Hindi – Counting और Binary Semaphore - General Topics
  • • अवलोकन: Quick Answer: Operating system में Semaphore एक integer variable है जिससे कई processes के बीच synchronization किया जाता है, ताकि shared resource या crit...
  • • मुख्य अवधारणाएं: Semaphores, Binary counting initialization Hindi, Producer consumer semaphores,Semaphores, Binary counting initialization, Producer consumer semaphores
Quick Answer: Operating system में Semaphore एक integer variable है जिससे कई processes के बीच synchronization किया जाता है, ताकि shared resource या critical section को एक समय में तय संख्या से ज़्यादा processes इस्तेमाल न करें। इसे सिर्फ दो atomic operations से बदला जाता है: wait() (P), जो value घटाता है, और signal() (V), जो value बढ़ाता है। Semaphore दो तरह के होते हैं: Binary semaphore (0 या 1) और Counting semaphore (कोई भी integer value)।

Semaphore in OS क्या है? (Semaphore Meaning)

"Semaphore" शब्द का आम मतलब है signal देने वाला निशान, जैसे railway का signal जो बताता है कि track खाली है या नहीं। OS में भी semaphore यही करता है: यह process को बताता है कि resource अभी इस्तेमाल हो सकता है या उसे रुकना होगा।

Semaphore का idea Edsger Dijkstra ने दिया था। इसके ज़रिए OS ये दो काम करता है:

  • Mutual exclusion: एक समय में सिर्फ एक process critical section में जाए।
  • Synchronization (order): एक process का काम दूसरे process के काम के बाद ही हो।

Critical section वो code है जिसमें process shared data (जैसे कोई common variable या file) बदलता है। अगर दो processes एक साथ उसे बदलें, तो result गलत आ सकता है। इसे race condition कहते हैं। Semaphore इसी को रोकता है। Critical section का concept detail में: Critical Region in Hindi.

Semaphores in Hindi

Semaphore का diagram: processes wait() से resource लेते हैं और signal() से छोड़ते हैं।

Advertisement

Semaphore के Operations: wait() और signal()

Semaphore S को initialize करने के बाद, उसकी value सिर्फ दो operations से बदल सकती है। दोनों atomic होते हैं, यानी जब एक process S की value बदल रहा हो, तब कोई दूसरा process बीच में S को नहीं छू सकता।

Operationदूसरे नामक्या करता हैकब call होता है
wait(S)P(S), down(S)S की value 1 घटाता है; resource नहीं है तो process को रोकता हैCritical section में जाने से पहले
signal(S)V(S), up(S)S की value 1 बढ़ाता है; रुका हुआ process हो तो उसे जगाता हैCritical section से निकलने के बाद

P और V Dutch शब्दों से आए हैं: P = proberen (test करना), V = verhogen (बढ़ाना)।

Definition 1: Busy Waiting वाला semaphore

wait(S) {
    while (S <= 0)
        ;        // कुछ नहीं करो, बस loop में रुको
    S--;
}

signal(S) {
    S++;
}

इसमें S कभी negative नहीं होता। Problem यह है कि रुका हुआ process loop में CPU बर्बाद करता रहता है। इसे busy waiting कहते हैं, और ऐसे semaphore को spinlock भी कहते हैं।

Definition 2: Blocking (Waiting Queue) वाला semaphore

typedef struct {
    int value;
    struct process *list;   // रुके हुए processes की queue
} semaphore;

wait(semaphore *S) {
    S->value--;
    if (S->value < 0) {
        // इस process को S->list में डालो
        block();
    }
}

signal(semaphore *S) {
    S->value++;
    if (S->value <= 0) {
        // S->list से एक process P निकालो
        wakeup(P);
    }
}

इसमें रुका हुआ process CPU नहीं खाता, वो block होकर queue में सो जाता है। यहाँ value negative भी हो सकती है, और negative value का magnitude बताता है कि कितने processes queue में रुके हैं। जैसे value = −2 मतलब 2 processes wait कर रहे हैं।

Types of Semaphore in OS

1. Binary Semaphore

इसकी value सिर्फ 0 या 1 होती है। Mutual exclusion के लिए initial value 1 रखी जाती है (order तय करने के लिए 0, जैसा नीचे synch वाले example में है)। इसका मुख्य काम mutual exclusion है: एक समय में एक ही process critical section में।

semaphore mutex = 1;

do {
    wait(mutex);
        /* critical section */
    signal(mutex);
        /* remainder section */
} while (1);

पहला process wait(mutex) करता है, value 1 से 0 होती है और वो अंदर चला जाता है। दूसरा process wait(mutex) करेगा तो उसे रुकना पड़ेगा, जब तक पहला signal(mutex) न कर दे।

2. Counting Semaphore in OS

Counting semaphore की value किसी भी range में हो सकती है। इसे उतनी value से initialize करते हैं जितने resource instances हैं। जैसे lab में 3 printers हैं तो S = 3। हर process printer लेने से पहले wait(S) और छोड़ने के बाद signal(S) करता है। जब कोई printer खाली न हो (wait() के समय S ≤ 0), तो नए process को रुकना पड़ता है।

Solved Example: Counting Semaphore Trace (S = 3)

3 printers हैं, S = 3। पाँच events इस order में होते हैं। Blocking (Definition 2) वाला semaphore मानें:

EventS की valueक्या हुआ
शुरुआत3तीनों printers खाली
P1: wait(S)2P1 को printer मिला
P2: wait(S)1P2 को printer मिला
P3: wait(S)0P3 को printer मिला, अब कोई खाली नहीं
P4: wait(S)−1Value < 0, इसलिए P4 block होकर queue में गया
P1: signal(S)0Value ≤ 0, इसलिए queue से P4 को wakeup किया; P1 वाला printer अब P4 को

Busy waiting वाले version में P4 के समय S = 0 ही रहता, और P4 loop में घूमता रहता जब तक P1 signal न करे।

Binary vs Counting Semaphore

PointBinary SemaphoreCounting Semaphore
Value0 या 1कोई भी integer (initial value = resources की संख्या)
Main useMutual exclusionएक resource के कई instances को control करना
एक साथ कितने process अंदरसिर्फ 1Initial value जितने
ExampleShared variable update करना3 printers, 5 database connections

Semaphore से Order तय करना (Synchronization Example)

मान लें process P2 का statement S2, process P1 के statement S1 के बाद ही चलना चाहिए। Semaphore synch = 0 लें:

/* P1 */            /* P2 */
S1;                 wait(synch);
signal(synch);      S2;

synch = 0 है, इसलिए P2 पहले आ भी जाए तो wait(synch) पर रुकेगा। P1 जब S1 पूरा करके signal(synch) करेगा, तभी P2 आगे बढ़ेगा। इस तरह S2 हमेशा S1 के बाद ही चलेगा।

Advertisement

Classic Problem: Producer–Consumer (Bounded Buffer)

Buffer में n slots हैं। Producer item डालता है, consumer item निकालता है। तीन semaphores लगते हैं:

  • mutex = 1: buffer को एक समय में एक ही process छुए।
  • empty = n: कितने slots खाली हैं।
  • full = 0: कितने slots भरे हैं।
/* Producer */              /* Consumer */
wait(empty);                wait(full);
wait(mutex);                wait(mutex);
  /* item buffer में डालो */    /* item buffer से निकालो */
signal(mutex);              signal(mutex);
signal(full);               signal(empty);

Buffer भरा होगा (empty = 0) तो producer रुकेगा। Buffer खाली होगा (full = 0) तो consumer रुकेगा। Processes आपस में data कैसे share करते हैं, यह यहाँ है: Inter Process Communication in Hindi.

Binary Semaphore और Mutex में फ़र्क

बहुत सी books binary semaphore को mutex कह देती हैं, क्योंकि दोनों mutual exclusion के लिए इस्तेमाल होते हैं। पर एक बड़ा फ़र्क है: mutex का एक owner होता है। जिस process/thread ने mutex lock किया, वही उसे unlock कर सकता है। Semaphore का कोई owner नहीं होता, कोई भी process signal() कर सकता है। इसीलिए ऊपर वाला "order तय करने" वाला काम semaphore से होता है, mutex से नहीं।

Semaphore की Problems: Deadlock और Starvation

Semaphore अपने आप deadlock नहीं रोकता। गलत order में wait() लगाने से deadlock हो सकता है। S = 1 और Q = 1 लें:

/* P0 */          /* P1 */
wait(S);          wait(Q);
wait(Q);          wait(S);
  ...               ...
signal(S);        signal(Q);
signal(Q);        signal(S);

P0 ने S लिया और P1 ने Q लिया। अब P0, Q का इंतज़ार कर रहा है और P1, S का। दोनों हमेशा के लिए रुक गए: यही deadlock है। Deadlock की चारों conditions detail में: Deadlock Characterization.

Starvation: अगर semaphore की waiting queue LIFO (last in, first out) order में चलाई जाए, तो पहले आया कोई process हो सकता है कभी जगे ही नहीं। इसलिए queue आमतौर पर FIFO रखी जाती है।

Advantages और Disadvantages of Semaphore

AdvantagesDisadvantages
Mutual exclusion आसानी से मिलता है, race condition रुकती हैwait/signal का order गलत हुआ तो deadlock
Counting semaphore से resource के कई instances संभलते हैंsignal() भूल गए तो बाकी processes हमेशा रुके रहेंगे
Blocking version में busy waiting नहीं, CPU बचता हैBusy waiting version में CPU time बर्बाद होता है
Processes का order (synchronization) भी तय कर सकते हैंबड़े programs में debugging मुश्किल; एक गलती पूरा program रोक सकती है

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Semaphore क्या है? इसके दो operations के नाम लिखिए। (wait/P और signal/V)
  2. Binary और counting semaphore में अंतर बताइए।
  3. Busy waiting क्या है? (Process का loop में CPU इस्तेमाल करते हुए रुके रहना)
  4. Counting semaphore S = 4 है। 6 processes wait(S) करें, तो blocking semaphore में S की value कितनी होगी? (4 − 6 = −2, यानी 2 processes queue में)
  5. Binary semaphore और mutex में एक अंतर बताइए। (Mutex का owner होता है)

Long Questions (5–10 marks)

  1. Semaphore क्या है? wait() और signal() operations की definition लिखिए और types समझाइए।
  2. Semaphore से critical section problem कैसे solve होती है? Producer–consumer problem का solution लिखिए।
  3. Semaphore के इस्तेमाल से deadlock और starvation कैसे हो सकते हैं? Example के साथ समझाइए।

FAQs

Semaphore meaning in Hindi क्या है?

Semaphore का मतलब है signal या संकेत देने वाला निशान। OS में यह एक integer variable है जो processes को बताता है कि resource मिलेगा या रुकना होगा।

Semaphore की value negative हो सकती है क्या?

Blocking (waiting queue) वाले implementation में हाँ। Negative value बताती है कि कितने processes queue में रुके हैं। Busy waiting वाले simple version में value 0 से नीचे नहीं जाती।

wait() और signal() atomic क्यों होने चाहिए?

अगर दो processes एक साथ S की value पढ़कर बदलें, तो semaphore खुद race condition का शिकार हो जाएगा और दोनों critical section में घुस सकते हैं।

Counting semaphore को 1 से initialize करें तो क्या होगा?

तब वो binary semaphore की तरह ही काम करेगा: एक समय में सिर्फ एक process अंदर जा पाएगा।

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

Quick Answer: Operating system में Semaphore एक integer variable है जिससे कई processes के बीच synchronization किया जाता है, ताकि shared resource या critical section को एक समय में तय संख्या से ज़्यादा processes इस्तेमाल न करें।
Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स के पाठ्यक्रम में Semaphore in OS in Hindi – Counting और Binary Semaphore एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Semaphore in OS in Hindi – Counting और Binary Semaphore के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Semaphore in OS in Hindi – Counting और Binary Semaphore
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
Threads 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
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