GATE CS 2021 Set 1 — Question 56
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MSQ+2 / -0HardSemaphoresProcess SynchronizationOperating System
Operating System → Process Synchronization → Semaphores
Last updated
Question
Consider the following pseudocode, where 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.If five threads execute the function parop concurrently, which of the following program behavior(s) is/are possible?
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.}
Correct 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.
Solution
1.Deadlock Analysis: Each thread requires 2 units of the semaphore to proceed past line 6. The semaphore is initialized to 5. If all 5 threads execute line 5 concurrently, they each acquire 1 unit, reducing to 0. Consequently, all 5 threads will block at line 6, waiting for a unit that will never be released. Thus, a deadlock is possible. (Option D is correct).
2.Counter Value Analysis: If the threads do not deadlock (e.g., they execute sequentially or in a manner that allows some to finish), they will each attempt to increment the counter.
- Since the increment
counter++is not atomic (it involves a read, increment, and write), standard race conditions apply. - If they execute sequentially, the final value will be 5. (Option A is correct).
- If they execute concurrently, a thread might read the initial value 0, then other threads finish their increments (bringing the counter to 4), and finally the first thread writes back its calculated value (0 + 1 = 1). Thus, the final value can be 1. (Option B is correct).
- A value of 0 is impossible if all threads successfully complete, as at least one write-back of a value must occur. (Option C is incorrect).
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on Process Synchronization
2026 Set 2 Q23Which one of the following CPU scheduling algorithms cannot be preemptive?2026 Set 1 Q29With respect to deadlocks in an operating system, which of the following statements is/are FALSE?2026 Set 1 Q31In the context of relational database normalization, which of the following statements is/are true?2026 Set 1 Q35Consider a system consisting of instances of a resource , being shared by 5 processes.…2026 Set 2 Q51Consider three processes P1, P2, and P3 running identical code, as shown in the pseudocode below. A…