Question
A shared variable , initialized to zero, is operated on by four concurrent processes as follows. Each of the processes and reads from memory, increments by one, stores it to memory, and then terminates. Each of the processes and reads from memory, decrements by two, stores it to memory, and then terminates. Each process before reading invokes the P operation (i.e., wait) on a counting semaphore and invokes the V operation (i.e., signal) on the semaphore after storing to memory. Semaphore is initialized to two. What is the maximum possible value of after all processes complete execution?
Official answer and solution
- Official answer
- D
- From
- Official GATE 2013 answer key
Solution
Find an interleaving of the four concurrent processes that maximizes the final value of x, leveraging the fact that semaphore S=2 allows two concurrent processes inside the critical region.
Initial conditions:
- Variable .
- Counting semaphore .
- Four processes:
- : reads , increments by 1, writes .
- : reads , increments by 1, writes .
- : reads , decrements by 2, writes .
- : reads , decrements by 2, writes .
Each process executes before reading and after storing .
Because is initialized to 2, two processes can be concurrently inside the critical region. To maximize the final value of , we want the decrements by and to be completely overwritten by an increment:
- Process executes (now ). reads into its local register. calculates local value , but is preempted before writing back to .
- Process executes (now ). reads , computes , writes , and executes (now ).
- Process executes (now ). reads , computes , writes , and executes (now ).
- Now process resumes. writes its computed local value to memory: . Then executes (now ). At this point, both decrements from and have been completely overwritten!
- Finally, process executes sequentially: (now ), reads , computes , writes , and executes (now ).
The maximum possible final value of is 2.
D
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2013 Computer Science and Information Technology (CS)
- Question
- 34 of 65, printed as CS Q.34
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.