Process 1 and Process 2 are concurrent processes that share variables A and B. J
ID: 3711413 • Letter: P
Question
Process 1 and Process 2 are concurrent processes that share variables A and B. Jack has developed what he thinks is a satisfactory program synchronizing the sharing. The relevant part of the program is shown below. Jill looks at the program and tells Jack that she things there are problems with his solution. Do you agree with Jack or Jill? Explain your decision. if you think there are problems, explain what they are; if not, justify your belief that Jack has written a correct program.
shared int A=0; B=0; //shared variables
semaphore semA=1; semB=1; //declare and initialize semaphores to protect variables
Process 1
Process 2
...
...
P(semA);
cin>>x>>y;
cin>>A;
P(semB);
...
B=x+y;
P(semB);
P(semA);
A=B+3;
B=A+B;
V(semB);
V(semA);
V(semA);
V(semB);
Process 1
Process 2
...
...
P(semA);
cin>>x>>y;
cin>>A;
P(semB);
...
B=x+y;
P(semB);
P(semA);
A=B+3;
B=A+B;
V(semB);
V(semA);
V(semA);
V(semB);
Explanation / Answer
The solution is not correct and Jill is correct because the solution is prone to deadlock.
To see this consider the following sequence of assigning the processes:
Process 1:
P(semA) // Therefore now semA=0
cin>>A // Process takes input A with value 0
Process 2:
cin>>x>>y // process takes input x and y
P(semB) // Therefore semB=0 now
B=x+y // B is updated from 0 to x+y
Process 1:
P(semB) // gets blocked as semB is made 0 by Process 2 and waits for Process 2 to finish
Process 2:
P(semA) // gets blocked as semA is made 0 by Process 1 and waits for Process 1 to finish
Therefore is DEADLOCK.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.