Question:medium

Consider the following pseudocode, where S is a semaphore initialized to 5 in line#2 and counter is a shared variable initialized to 0 in line #1. Assume that the increment operation in line#7 is not atomic. 

1. int counter = 0; 
2. Semaphore S = init(5); 
3. void parop(void) 
4. { 
5.     wait(S); 
6.     wait(S); 
7.     counter++; 
8.     signal(S); 
9.     signal(S); 
10. } 

If five threads execute the function parop concurrently, which of the following program behavior(s) is/are possible?
 

Show Hint

Counting semaphores limit concurrency but do not guarantee mutual exclusion unless carefully designed. Non-atomic operations inside critical sections can still cause race conditions.
Updated On: Jan 30, 2026
  • The value of counter is 5 after all the threads successfully complete the execution of \texttt{parop}.
  • The value of counter is 1 after all the threads successfully complete the execution of \texttt{parop}.
  • The value of counter is 0 after all the threads successfully complete the execution of \texttt{parop}.
  • There is a deadlock involving all the threads.
Show Solution

The Correct Option is A, B, D

Solution and Explanation

Step 1: Semaphore behavior.
The semaphore S is initialized to 5. Each thread executes wait(S) twice before entering the critical section. Hence, each thread requires 2 units of the semaphore to proceed.

With 5 threads, the total required semaphore units are: \[ 5 \times 2 = 10 \] But only 5 units are available initially. This mismatch creates the possibility of blocking.

Step 2: Possibility of deadlock (Option D).
A deadlock can occur if all five threads successfully execute their first wait(S), reducing the semaphore value from 5 to 0. Then, each thread blocks on its second wait(S). Since no thread reaches the signal(S) statements, no semaphore units are released.

Thus, a deadlock involving all threads is possible. So, Option (D) is possible.

Step 3: Effect of non-atomic increment.
The statement counter++ is not atomic; it consists of read, increment, and write operations. Although the semaphore restricts concurrency, it does not ensure mutual exclusion for the increment itself.

Therefore, race conditions can occur.

Step 4: Analysis of Option (A).
In a favorable execution order where threads increment counter without harmful interleavings, all five increments may be applied correctly.

Hence, the final value of counter can be: \[ 5 \] So, Option (A) is possible.

Step 5: Analysis of Option (B).
Due to race conditions, multiple threads may read the same value of counter before writing back. In the worst case, all threads read 0 and finally write back 1.

Thus, the final value of counter can be: \[ 1 \] So, Option (B) is also possible.

Step 6: Analysis of Option (C).
If all threads complete execution of the procedure, each thread executes counter++ at least once.

Therefore, the value of counter cannot remain 0. So, Option (C) is not possible.

Final Conclusion:
The possible behaviors are: \[ \boxed{(A),\ (B),\ \text{and } (D)} \]

Was this answer helpful?
0