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

Unit 7 Assignment – Process Synchronization explain whether this code satisfied

ID: 673078 • Letter: U

Question

Unit 7 Assignment – Process Synchronization

explain whether this code satisfied mutual exclusion, progress, and bounded waiting for the critical section problem. Also explain why the code allows it to, not just give a repetitive definition of the question...

boolean flag[2];

int turn;

Process P0

Process P1

while(1){

while(1){

flag[0]=true;

flag[1]=true;

While(flag[1]){

while(flag[0]){

   If(turn==1){

   if(turn==0){

      flag[0]=false;

      flag[1]=false;

      while(turn==1);

      while(turn==0);

      flag[0]=true;

      flag[1]=true;

   }

   }

}

}

critical section

critical section

turn=1;

turn=0;

flag[0]=false;

flag[1]=false;

}

}

explain whether this code satisfied mutual exclusion, progress, and bounded waiting for the critical section problem. Also explain why the code allows it to, not just give a repetitive definition of the question...

boolean flag[2];

int turn;

Process P0

Process P1

while(1){

while(1){

flag[0]=true;

flag[1]=true;

While(flag[1]){

while(flag[0]){

   If(turn==1){

   if(turn==0){

      flag[0]=false;

      flag[1]=false;

      while(turn==1);

      while(turn==0);

      flag[0]=true;

      flag[1]=true;

   }

   }

}

}

critical section

critical section

turn=1;

turn=0;

flag[0]=false;

flag[1]=false;

}

}

Explanation / Answer

Hi,

Below is the solution to your problem:

Consider both the proccesses P0 and P1

I am going to explain the sequence of instructions buy the following execution sequence:

P0 P1 Comments

(1) flag[0] is set to true;

(2) P0 exits the first while-loop in the entry section

(6) P0 and turn==1 P1 critical section; since flag[0]=false;

context switch

(1) flag[1] is set to true and turn ==0

(2)-(3) flag[1] is set to false

(4) P1 enters the second while-loop in the entry section

context switch

(7) flag[0] is set to false

(8) P0 executes its non-critical section

(1) flag[0] is set to true

(2) P0 exits the first while-loop in the entry section

(6) P0 enters its critical section; flag[0] is true

context switch

(4) P1 is still waiting for flag[0] to be false

context switch

Hence the above metioned the code satisfies all three conditions.

Hope that solves your problem...HAPPY ANSWERING!!!