Question

In: Computer Science

Question Four Explain Peterson’s solution for critical-section problem and show that mutual exclusion is preserved with...

Question Four Explain Peterson’s solution for critical-section problem and show that mutual exclusion is preserved with Peterson’s solution. (Assume there are only two processes P0 and P1)

Solutions

Expert Solution

Peterson's Algorithm:

  • The solution is limited to two processes alternating the execution of their critical sections with the remaining sections. Assume Process numbers are P1 and P2.
  • Needs the two processes that share two pieces of data.

int turn;

boolean flag[2];

  • The variable turn denotes the turn of whose critical section it is to enter i.e. if turn == x, then process Px can be executed in the critical section.
  • If a process is ready to enter its critical section, this is indicated through the flag array. For example, if the flag[x] is true, this value shows Px is ready to enter its critical section.

Initialization:

flag[1] := FALSE;

flag[2] := FALSE;

turn := random(1..2)

Algorithm:

//declaring do-while loop to continue execution until the condition is true

do {

   //flag variable for x is true which indicates that process x has requested for

   //critical section.

   flag[x] = true;

   //turn = y indicates that y is in critical section

   turn = y;

   //using while condition to make process x wait until process y is done with its critical         //section

   while (flag[y] && turn == y);

   /* executing the critical section */

   flag[x] = false;// its indicates that x is done with its critical section hence its

   //flag value is changed

   /* executing the remainder section */

}

//end of do-while loop

while (true);

Proof that mutual exclusion is preserved in Peterson's Solution:

  • Condition for each Px to enter its critical section is:

flag[y] == FALSE i.e. process y has no intention to enter in critical section

OR

turn == x i.e. its turn of process x to enter in critical section.

  • If both can run concurrently in their critical sections then flag[1 ] = = flag[2 ] = = TRUE

From the above two observations, we get that both the process P1 and P2 cannot execute there while the statement at the same time as the value of turn variable can be 1 or 2 but cannot be both. Therefore only one of process among them let Py will be executing there while statement successfully

Therefore, one of the processes — say, Py — must have executed the while statement successfully, while Px has to execute at least one additional statement i.e. (turn = = y). Nonetheless, at that time flag[y] = = true and turn = = y, and this state will continue as long as Py is in its critical section; as a result, it maintains the mutual exclusion.


Related Solutions

. Is the following software solution to the critical section problem correct?                      {          &nbsp
. Is the following software solution to the critical section problem correct?                      {              int   lock = 0;                ………….                if (lock == 0) then lock = 1                 else                         while (lock == 1) no-op;                   enter_CS;                          CS                    exit_CS;                 ……….             }
There are three ways to provide mutual exclusion in a system. Explain how mutual exclusion can...
There are three ways to provide mutual exclusion in a system. Explain how mutual exclusion can be guaranteed using hardware approach and software approach.
A critical section problem can be solved by satisfying which of the following condition, a. Mutual...
A critical section problem can be solved by satisfying which of the following condition, a. Mutual Exclusion b. Progress c. Bounded Waiting d. All of the mentioned Give a logical reason with an example for it why and how you selected it?
Consider the four distributed mutual exclusion algorithms Permission Based ---#1: Centralized Mutual Exclusion ---#2: Decentralized Mutex...
Consider the four distributed mutual exclusion algorithms Permission Based ---#1: Centralized Mutual Exclusion ---#2: Decentralized Mutex Algorithm ---#3: Distributed Mutual Exclusion #4: A Token Ring Algorithm Discuss their relative fault tolerance – basically the effect of processor crashes on the performance of the algorithm. You don’t have to come up with a 1-2-3-4 ordering.
Modify the included program to implement the strict alternation solution to achieve mutual exclusion. It does...
Modify the included program to implement the strict alternation solution to achieve mutual exclusion. It does not matter whether Thread 0 goes first or Thread 1, but it is important that the thread strictly alternate. PROGRAM: #include <iostream> #include <pthread.h> #include <stdlib.h> int count; int turn = 0; // Shared variable used to implement strict alternation void* myFunction(void* arg) {    int actual_arg = *((int*) arg);       for(unsigned int i = 0; i < 10; ++i) {    //...
Operating systems Explain what the critical section problem is. Please explain this in about 1-2 paragraphs.
Operating systems Explain what the critical section problem is. Please explain this in about 1-2 paragraphs.
Single Lane Bridge Problem : Java Threads Given : //Use ReentrantLock for mutual exclusion import java.util.concurrent.locks.Lock;...
Single Lane Bridge Problem : Java Threads Given : //Use ReentrantLock for mutual exclusion import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class bridge { private final ReentrantLock myLock = new ReentrantLock(); public bridge(){ } public String cross(){ myLock.lock(); try { return "crossing the bridge"; } finally { myLock.unlock(); } } } public class lane extends Thread { int cars; bridge Bridge; public lane(int Cars1, bridge Bridge1) { cars = Cars1; Bridge = Bridge1; } public void run(){ for(int x=0;x //Important clue :...
Choose one of the four questions presented in the "critical thinking" section on page 87 of...
Choose one of the four questions presented in the "critical thinking" section on page 87 of the textbook. Present your answers in an essay format with professional writing and well thought out answers. Also provide some research and state a fact or idea. For example the sky is blue ( and here is why) and according to Dr. Smith in his article (abcdefgh) he states the the blue sky comes from...... The question is What can businesses do to prevent...
4.1) What three conditions must be satisfied in order to solve the critical section problem and...
4.1) What three conditions must be satisfied in order to solve the critical section problem and why? 4.2) Please answer each of the following questions briefly: a)   What is deadlock avoidance? (2 points) b)   What is deadlock prevention? (2 points) c)   Please discuss a strategy for deadlock avoidance (3 points) d)   Please discuss a strategy for deadlock prevention (3 points) 5. (Chapter 6) Please answer the following questions briefly (5 points each, total 10 points) 5.1) Explain the process of...
Note : I need word typed solution for this problem with comprehensive details. Question : Explain...
Note : I need word typed solution for this problem with comprehensive details. Question : Explain the thermal, acoustic and optical properties of lattice vibrations? Please make it to explain all in need.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT