Gate
Exam
A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution?
Option
(A) -2
(B) -1
(C) 1
(D) 2
Read Solution (Total 1)
-
- Key point: Semaphore S is initialized to two (Given in problem).
Solution:
-Maximum two processes are allowed to enter in to critical section
- There are three statements to be execute by first and second process concurrently .
- Order of exection of processes represent in brackets
- Below is one of the solution to get maximum x value.
w y z x
Read 0(1) 0(2) 1(6) 1(7)
Inc/Dec 1(5) -2(3) 1(10) -1(8)
Store 1(6) -2(4) 2(11) -1(9)
1.Consider w and y are in critical section.
a.W process read value of x(0) and then preempted.
b.Y process read value of x ,decrement value of x to -2 and stores it in memory.
c.After completion of y. w process increments value of x to 1 and stores it in memory .
d. Now value of x in memory is 1
2. Now z and x in critical section
a.Z process read value of x(1) and then preempted.
b.X process read value of x ,decrement value of x to -1 and stores it in memory.
c.After completion of X. Z process increments value of x to 2 and stores it in memory .
d. Now value of x in memory is 2
FINAL VALUE IS 2
- 10 years agoHelpfull: Yes(0) No(1)
Gate Other Question