0
0
Operating Systemsknowledge~20 mins

Semaphores (counting and binary) in Operating Systems - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Semaphore Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Counting Semaphore Behavior

What happens when a counting semaphore initialized to 3 is signaled (V operation) twice after three wait (P) operations have already been performed?

AThe semaphore value becomes 5, which is invalid because it exceeds the initial count.
BThe semaphore value remains 0, so no additional wait operations can proceed without blocking.
CThe semaphore value becomes 2, allowing two more wait operations without blocking.
DThe semaphore value becomes 1, allowing one more wait operation without blocking.
Attempts:
2 left
💡 Hint

Remember that signaling a counting semaphore increments its value, allowing more processes to enter the critical section.

📋 Factual
intermediate
2:00remaining
Binary Semaphore Characteristics

Which of the following statements correctly describes a binary semaphore?

AIt can only have values 0 or 1 and is used to enforce mutual exclusion.
BIt automatically resets to 0 after each signal operation.
CIt allows multiple processes to enter the critical section simultaneously.
DIt can have any non-negative integer value and is used to count resources.
Attempts:
2 left
💡 Hint

Think about the simplest semaphore used for locking.

🚀 Application
advanced
2:00remaining
Semaphore Usage in Producer-Consumer Problem

In a producer-consumer problem using semaphores, which semaphore should the consumer wait on before consuming an item?

AThe semaphore counting the number of empty slots in the buffer.
BThe semaphore counting the number of full slots in the buffer.
CA binary semaphore used to protect the buffer access.
DA semaphore initialized to zero that counts the number of producers.
Attempts:
2 left
💡 Hint

Consumers can only consume if there is at least one item available.

🔍 Analysis
advanced
2:00remaining
Deadlock Scenario with Semaphores

Which of the following semaphore usage patterns can cause a deadlock?

AA counting semaphore is initialized to a value greater than the number of processes.
BA process signals a semaphore before waiting on it.
CA binary semaphore is used to protect a critical section accessed by one process only.
DTwo processes each wait on a semaphore held by the other, causing circular wait.
Attempts:
2 left
💡 Hint

Consider what happens when two processes wait indefinitely for each other.

Reasoning
expert
2:00remaining
Effect of Incorrect Semaphore Initialization

What is the most likely outcome if a binary semaphore used for mutual exclusion is incorrectly initialized to 2 instead of 1?

AMultiple processes may enter the critical section simultaneously, breaking mutual exclusion.
BThe semaphore will cause a deadlock immediately after the first wait operation.
CThe semaphore will automatically reset to 1 after the first signal operation.
DThe semaphore will behave like a counting semaphore with no effect on mutual exclusion.
Attempts:
2 left
💡 Hint

Think about what happens if the lock allows more than one holder.