Home » Operating Systems » Operating systems miscellaneous » Question

Operating systems miscellaneous

  1. A shared variable x, initialised 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 initialised to two. What is the maximum possible value of x after all processes complete execution?
    1. –2
    2. –1
    3. 1
    4. 2
Correct Option: D

Each of the processes W and X reads x from memory and .increment x by .1.
Each of the processes Y and Z reads x from memory, and decrement by 2.
1. Start with X and perform P(S) then S = 1 read x = 0 x = x + 1 = 1
2. Then Y will perform P(S) then S = 0 read X = 0 x = x–2 = –2 then store x. V(S),S = 1
3. Then Z will perform P(S) then S = 0, read x = – 2 x = x – 2 = – 4 then store x, V(S).S = 1.
4. Then x will store x.V(S), S = 2, X = 1
5. Then W will perform P(S),S = 1, read x = 1 x = x + 1 = 2, store x, V(S),S = 2, x = 2
Hence, answer is option (d)



Your comments will be displayed only after manual approval.