Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

GATE | GATE CS 2021 | Set 1 | Question 56

  • Difficulty Level : Hard
  • Last Updated : 24 May, 2021

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?
(A) The value of counter is 5 after all the threads successfully complete the execution of parop
(B) The value of counter is 1 after all the threads successfully complete the execution of parop
(C) The value of counter is 0 after all the threads successfully complete the execution of parop
(D) There is a deadlock involving all the threads


Answer: (A) (B) (D)

Explanation: First of all, since the value of counter is 5, and if there are two wait() and two signal() for each thread. So, if when these threads will be completed, final counter value can never be 0.
So, option (C) is false.

Counter value 5 is possible if threads run in any sequence, i.e., one by one.
So, option (A) is true.

Counter value 1 is also possible if a thread which is entered in first and read the counter = 0 and writing counter value 5 at the end of after executing all other threads.
So, option (B) is true.

Five threads getting pre-empted after executing only first wait(S); line-5.
Now, second wait(S); line-6, will cause deadlock.
So, option (D) is true.

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!