GATE CS 2021 (Forenoon) — Question 56

Operating Systems (OS)Concurrency & Synchronization (Semaphores, Mutex, Monitors, Locks)MSQ2 marksMedium

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.
c
11. int counter = 0;
22. Semaphore S = init(5);
33. void parop(void)
44. {
55.     wait(S);
66.     wait(S);
77.     counter++;
88.     signal(S);
99.     signal(S);
1010. }
11
If five threads execute the function parop concurrently, which of the following program behavior(s) is/are possible?
  1. A
    The value of counter is 5 after all the threads successfully complete the execution of parop.
  2. B
    The value of counter is 1 after all the threads successfully complete the execution of parop.
  3. C
    The value of counter is 0 after all the threads successfully complete the execution of parop.
  4. D
    There is a deadlock involving all the threads.
Practise the full 2021-FN paper
Challenge a friend: WhatsApp Telegram
From RENYXERAFree All-India Mock — every SundayA full GATE CS paper, exactly 180 minutes, ranked across India with your GATE score.
Practise more on Concurrency & Synchronization (Semaphores, Mutex, Monitors, Locks)Timed, in the real exam interface · free to start

A GATE question every morning

One real past-paper question every morning for your branch, answer hidden until you tap. Free on Telegram.

Join
Stuck? Ask in the student group