GateAI
Concurrency and Synchronization

GATE CSE 2021 Set 1 · Q46

MSQ2 marksMedium

Time

Consider the following pseudocode, where SS 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?

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

Official answer: 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 .; D) There is a deadlock involving all the threads.

Concurrency and Synchronization

Let us analyze the possible executions when five threads run parop concurrently:

  1. Option D (Deadlock):

    • The initial semaphore value is S=5S = 5.

    • If all 55 threads execute the first wait(S) at line 5 concurrently, each decrements SS by 11. The value of SS becomes 5−5=05 - 5 = 0.

    • When all 55 threads subsequently attempt to execute the second wait(S) at line 6, all of them block because S=0S = 0.

    • No thread can proceed to execute signal(S), leading to a deadlock involving all five threads. Hence, (D) is possible.

  2. Option A (Counter = 5):

    • If all five threads execute sequentially one after another without preemption during critical operations, each thread successfully increments counter by 11.

    • After all 5 threads finish, the final value of counter is 55. Hence, (A) is possible.

  3. Option B (Counter = 1):

    • The operation counter++ is non-atomic and consists of three machine instructions: LOAD R, counter, INCR R, and STORE counter, R.

    • Since S=5S = 5, two threads (say T1T_1 and T2T_2) can acquire two wait(S) each simultaneously (2+2=4≤52 + 2 = 4 \le 5).

    • T1T_1 reads counter = 0, increments its local register to 11, and is then preempted before storing.

    • The remaining threads (T2,T3,T4,T5T_2, T_3, T_4, T_5) execute one by one and increment counter up to 44.

    • Finally, T1T_1 resumes and stores 11 into counter, overwriting the previous value.

    • Hence, a final value of 11 is possible, so (B) is possible.

  4. Option C (Counter = 0):

    • For all threads to successfully complete execution, each thread must store at least R+1≥1R + 1 \ge 1 into counter at its completion. It is impossible for counter to remain 00 after successful completion of all threads. Thus, (C) is not possible.

Therefore, the correct options are A, B, and D.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Concurrency and Synchronization previous year question is Q46 of the GATE CSE 2021 Set 1 paper. Find more GATE CSE PYQs on Concurrency and Synchronization in Operating Systems, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Concurrency and Synchronization PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Concurrency and Synchronization in GATE CSE

Concurrency and Synchronization has 18 GATE CSE PYQs across 15 papers from 2012 to 2026. 4 of them are 2-mark MSQs like this one, and it is the only one in GATE CSE 2021 Set 1.

How GATE tests Concurrency and Synchronization

More from GATE CSE 2021 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2021 Set 1 · Q46 · MSQ

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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.