A semaphore is a tool used to control access to shared resources by multiple processes to avoid conflicts and ensure safe execution.
Click to reveal answer
beginner
What is the difference between a counting semaphore and a binary semaphore?
A counting semaphore can have any non-negative integer value and controls access to multiple instances of a resource. A binary semaphore only has two values (0 or 1) and controls access to a single resource, acting like a simple lock.
Click to reveal answer
intermediate
How does a process use a semaphore to enter a critical section?
The process performs a 'wait' operation on the semaphore. If the semaphore value is positive, it decreases the value and enters the critical section. If zero, the process waits until the semaphore is available.
Click to reveal answer
intermediate
What happens when a process finishes using a resource controlled by a semaphore?
The process performs a 'signal' operation, which increases the semaphore value and may wake up a waiting process to allow it to enter the critical section.
Click to reveal answer
beginner
Why are semaphores important in operating systems?
Semaphores help prevent race conditions and ensure that multiple processes can safely share resources without interfering with each other.
Click to reveal answer
What value range does a counting semaphore hold?
AAny non-negative integer
BOnly 0 or 1
COnly positive integers
DOnly negative integers
✗ Incorrect
Counting semaphores can hold any non-negative integer to represent multiple available resources.
Which semaphore type acts like a simple lock?
AMutex semaphore
BCounting semaphore
CBinary semaphore
DSignal semaphore
✗ Incorrect
Binary semaphores have only two values (0 or 1) and act like locks to control access to a single resource.
What operation decreases the semaphore value when a process wants to enter a critical section?
ASignal
BRelease
CLock
DWait
✗ Incorrect
The 'wait' operation decreases the semaphore value if it is positive, allowing the process to enter.
What does the 'signal' operation do in semaphore usage?
ADecreases semaphore value
BIncreases semaphore value
CBlocks the process
DTerminates the process
✗ Incorrect
The 'signal' operation increases the semaphore value and may wake waiting processes.
Why are semaphores used in operating systems?
ATo prevent race conditions and manage resource sharing
BTo encrypt data
CTo store data permanently
DTo speed up CPU processing
✗ Incorrect
Semaphores help coordinate processes to avoid conflicts when sharing resources.
Explain how counting and binary semaphores differ and give a real-life example for each.
Think about how many resources each semaphore controls.
You got /4 concepts.
Describe the sequence of operations a process performs when using a semaphore to access a shared resource safely.
Focus on wait and signal steps.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a semaphore in operating systems?
easy
A. To manage user passwords
B. To increase the speed of the CPU
C. To store data permanently
D. To control access to shared resources and prevent conflicts
Solution
Step 1: Understand the role of semaphores
Semaphores are synchronization tools used to manage access to shared resources in concurrent systems.
Step 2: Identify the correct purpose
The main goal is to avoid conflicts by controlling how many processes can access a resource at once.
Final Answer:
To control access to shared resources and prevent conflicts -> Option D
Quick Check:
Semaphore controls resource access = A [OK]
Hint: Semaphores manage resource access to avoid conflicts [OK]
Common Mistakes:
Confusing semaphores with memory storage
Thinking semaphores speed up CPU
Mixing semaphores with security functions
2. Which of the following is the correct way to initialize a binary semaphore with an initial value of 1?
easy
A. semaphore = Semaphore(0)
B. semaphore = Semaphore(2)
C. semaphore = Semaphore(1)
D. semaphore = Semaphore(-1)
Solution
Step 1: Recall binary semaphore initialization
A binary semaphore can only have values 0 or 1, where 1 means resource is available.
Step 2: Choose the correct initial value
Initializing with 1 means the resource is free initially, so Semaphore(1) is correct.
Final Answer:
semaphore = Semaphore(1) -> Option C
Quick Check:
Binary semaphore init with 1 = D [OK]
Hint: Binary semaphore starts at 1 to allow one user [OK]
Common Mistakes:
Using 0 which blocks access initially
Using negative values which are invalid
Using values greater than 1 for binary semaphore
3. Consider a counting semaphore initialized to 3. If three processes call wait() on it, what will be the semaphore's value afterwards?
medium
A. 0
B. -3
C. 3
D. 1
Solution
Step 1: Understand counting semaphore behavior
Each wait() call decreases the semaphore value by 1 if the resource is available.
Step 2: Calculate the new value after three waits
Starting at 3, after three wait() calls, value = 3 - 3 = 0.
Final Answer:
0 -> Option A
Quick Check:
3 - 3 waits = 0 [OK]
Hint: Subtract one per wait call from initial count [OK]
Common Mistakes:
Thinking semaphore goes negative immediately
Confusing wait() with signal() effects
Assuming semaphore resets automatically
4. A programmer wrote this code snippet using a binary semaphore:
A. It releases the semaphore twice causing an error
B. It causes a deadlock because the second wait() blocks indefinitely
C. It initializes the semaphore incorrectly
D. It does not use signal() after the first wait()
Solution
Step 1: Analyze the semaphore initial value and wait calls
The semaphore starts at 1, so the first wait() decreases it to 0 (resource taken).
Step 2: Understand the effect of the second wait()
The second wait() tries to decrease from 0, so it blocks waiting for signal().
Final Answer:
It causes a deadlock because the second wait() blocks indefinitely -> Option B
Quick Check:
Second wait on zero blocks = deadlock A [OK]
Hint: Second wait on binary semaphore without signal blocks [OK]
Common Mistakes:
Thinking signal() is called twice
Assuming semaphore initializes incorrectly
Ignoring blocking behavior of wait()
5. You have a counting semaphore initialized to 5 to control access to a printer shared by multiple users. If 7 users try to access the printer simultaneously, how does the semaphore manage access?
hard
A. Allows 5 users to access immediately; 2 users wait until others release
B. Allows all 7 users to access simultaneously
C. Blocks all users until the semaphore is reset
D. Allows only 1 user at a time regardless of initial value
Solution
Step 1: Understand counting semaphore limits
A counting semaphore with initial value 5 allows up to 5 concurrent accesses.
Step 2: Analyze user access requests
When 7 users request access, 5 proceed immediately; the remaining 2 must wait for signal() calls.
Final Answer:
Allows 5 users to access immediately; 2 users wait until others release -> Option A
Quick Check:
Counting semaphore limits concurrent users = B [OK]
Hint: Counting semaphore limits concurrent access to initial count [OK]
Common Mistakes:
Assuming all users access simultaneously ignoring limits
Confusing counting semaphore with binary semaphore
Thinking semaphore blocks all users at once
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.