0
0
Operating Systemsknowledge~10 mins

Semaphores (counting and binary) in Operating Systems - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Semaphores (counting and binary)
Start
Check semaphore value
Decrement semaphore
Allow access
Block process
Wait for semaphore increment
End
The semaphore controls access by checking its value. If positive, it decrements and allows access; if zero, the process waits until the semaphore is incremented.
Execution Sample
Operating Systems
semaphore = 2
process1: wait(semaphore)
process2: wait(semaphore)
process3: wait(semaphore)
process1: signal(semaphore)
Three processes try to enter a critical section controlled by a semaphore initialized to 2.
Analysis Table
StepProcessSemaphore Value BeforeActionSemaphore Value AfterProcess State
1process12wait: value>0, decrement1enters critical section
2process21wait: value>0, decrement0enters critical section
3process30wait: value=0, blocks0blocked, waiting
4process10signal: increment1exits critical section
5process31unblocked, wait: value>0, decrement0enters critical section
💡 All processes have entered or are waiting; semaphore controls access by blocking when value is zero.
State Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
semaphore210010
process1 statewaitingin critical sectionin critical sectionin critical sectionexitedexited
process2 statewaitingin critical sectionin critical sectionin critical sectionin critical sectionin critical section
process3 statewaitingwaitingwaiting (blocked)waiting (blocked)waiting (blocked)in critical section
Key Insights - 3 Insights
Why does process3 block at step 3 even though it called wait?
At step 3 in the execution_table, the semaphore value is 0, so process3 cannot decrement it and must wait until another process signals (increments) the semaphore.
What happens to the semaphore value when a process signals?
As shown at step 4, signaling increments the semaphore value by 1, allowing blocked processes to proceed.
How does a binary semaphore differ from a counting semaphore in this flow?
A binary semaphore only allows values 0 or 1, so it controls access for one process at a time, while a counting semaphore can have values greater than 1 to allow multiple simultaneous accesses.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the semaphore value and what happens to process3?
ASemaphore is 2; process3 enters critical section
BSemaphore is 0; process3 blocks waiting
CSemaphore is 1; process3 enters critical section
DSemaphore is 0; process3 exits
💡 Hint
Check the 'Semaphore Value Before' and 'Process State' columns at step 3 in execution_table.
At which step does the semaphore value increase?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for the 'signal' action in the 'Action' column in execution_table.
If the semaphore was initialized to 1 instead of 2, what would happen at step 2?
Aprocess2 would block waiting
Bprocess2 would enter critical section
Cprocess3 would enter critical section
Dsemaphore value would be 2
💡 Hint
Refer to the semaphore value changes in variable_tracker and how wait works when value is 0.
Concept Snapshot
Semaphore controls access to resources.
Counting semaphore: value ≥ 0, allows multiple accesses.
Binary semaphore: value 0 or 1, allows single access.
wait() decrements if >0, else blocks.
signal() increments and may unblock waiting processes.
Full Transcript
Semaphores are tools to control access to shared resources. They hold a value representing available resources. When a process wants access, it calls wait(). If the semaphore value is greater than zero, wait() decrements it and allows the process to enter. If zero, the process blocks until another process calls signal(), which increments the semaphore and may unblock waiting processes. Counting semaphores allow multiple simultaneous accesses, while binary semaphores allow only one. This flow ensures safe access and prevents conflicts.