Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the Peterson solution shown here to the mutual exclusion problem. int f

ID: 3680000 • Letter: C

Question

Consider the Peterson solution shown here to the mutual exclusion problem.

int flag[1] = 0, flag[2] = 0, turn;

                   P1:

                           …..

                           while (1) {

                                   flag[1] = 1;

                                   turn = 1;

                                   while (flag[2] && turn == 1);

                                   // critical section 1

                                   flag[1] = 0;

                                   // remainder section 1

                           }

                   P2:

                           …..

                           while (1) {

                                   flag[2] = 1;

                                   turn = 2;

                                   while (flag[1] && turn == 2);

                                   // critical section 2

                                   flag[2] = 0;

                                   // remainder section 2

                           }

(1) Assume process P1 is inside critical section 1. What are the values of flag[1], flag[2], and turn that prevent P2 from entering its critical section?

(2) Assume that both processes have just entered the while-loop immediately preceding their respective critical sections. What are the values of flag[1], flag[2], and turn at that point? What guarantees that exactly one process will be able to proceed?

(3) Assume process P1 terminates. What are the values of flag[1], flag[2], and turn that allow P2 to continue entering its critical section repeatedly?

Explanation / Answer

1)p1 proceed in critical section it means..flag[2]=0 then only p1 enter into it's critical section.

so,flag[1]=1

flag[2]=0

we are dong flag[1]=0 after p1 comes out from critical section.if we didn't do this then p2 cannot enter into critical section.because while it enters into critical section it checks flag[1].if we make flag[1]=0 it gets executed..if not it doesn't executed..

2)if both process wants to execute then flag[1]=1 and flag[2]=2 are set..

But,turn=1 and turn=2 it won't happen.if one process able to proceed then turn is set to either 1 or 2 so,any one process can get executed.then mutual exclusion is guaranteed..

3)if p1 process terminates then flag[1]=0,flag[1]=1.turn=2 .so,p2 get's into its's critical section repeatedly...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote