A shared variable A , initialized to 0, is operated on by 4 processes w,x,y,z. P
ID: 3798623 • Letter: A
Question
A shared variable A , initialized to 0, is operated on by 4 processes w,x,y,z. Processes w and x increment A by 1 while processes y and z decrement it by 2. Each process before reading performs a wait on a semaphore S and signal on S after store or writing.
Ex: Process W () {
wait(S);
Read A to a local var;
increment local var by 1;
Store value from local var to A.
signal(S);
}
Same code for process x. For y,z, the increment is changed to decrement by 2.
i. Find what would be the MAX value of A after all the 4 processes complete execution Fully, if Semaphore S were to be initialized with 1. Explain your answer with the ordering of the processes.
ii. What would be the MAX value of A after all 4 processes complete fully, if Semaphore S were to be initialized with value 2. Explain your answer with the ordering of the processes.
Explanation / Answer
(i) If semaphore is initialized with the 1,then processed would be executed correctly and then we get the final value of A as '-2'.
Answer : ' -2 '
(ii) Since initial value of semaphore is 2,
W and X be the processes. W and X incremented A by 1 and, y and Z decrement A by 2. So, the final value of A is 1 and not -1 and the two Signal operations make semaphore value 2 again.
Semaphore S is initialized to the value 2
Process W executes the S=1, A=1 but it does not update the A variable.
Then the process Y executes S=0, it decrements A, now A = -2 and
signal semaphore is S=1
Now process Z executes S=0, A=-4, signal semaphore S=1
Now process W updates the A=1, S=2
Then process A executes A=2
Answer :: ' 2 '
/// *** Thank You *** ///
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.