I completed number 1 but I have trouble with number 2. (a) Given the following s
ID: 3844628 • Letter: I
Question
I completed number 1 but I have trouble with number 2.
(a) Given the following solution for the critical section problem for two processes (the code shown is for processes Pi), show why it does not satisfy the mutual exclusion requirement. Here, turn, flag[i[], and flag[j] are is shared variables; flag[i] and flag[j] are initialized to FALSE and turn initialized to i. do {flag[i] = true; turn = i; while (flag[j] && turn == j); Critical section flag[i] = FALSE; Remainder section} (b) What happens if flag[i] and flag[j] are set to TRUE in the above code? Three processes P1, P2, and P3 are sharing a resource R1 in a mutually exclusive manner. Using test_and_set instruction, show code for P1, P2, and P3 that shows the entry section and exit section (similar to the code shown in Question 1). Using this code, show a scenario if all three processes want to enter their critical sections.Explanation / Answer
We can use peterson's algorithm , from a source i explained below on how to use it for single process and N processos ...apply same for 3 process (N=3)
The intuitive informal description of two process Peterson's algorithm for process 0 can be given as follows:
flag[0]=true; //I am ready to enter my critical region
turn=1; //but you may take your turn to enter your critical section
while(flag[1]==true && turn==1) //if you are ready to enter your critical section
//and if its your turn,
{ } //I will wait
//Critical Section
flag[0]=false; //I am no more ready to enter my critical section
Peterson's algorithm for n processes is given as follows:
Each process runs the following pseudo code:
lock(pid);
<critical section>;
unlock(pid);
where lock() and unlock() functions are defined as below..
lock(for Process i):
/* repeat for all partners */
for (count = 0; count < (NUMPROCS-1); count++) {
flags[i] = count;
turn[count] = i;
"wait until (for all k != i, flags[k]<count) or (turn[count] != i)"
}
Unlock (for Process i):
/* tell everyone we are finished */
flags[i] = -1;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.