The following is the structure of process P_i from an algorithm attempting to so
ID: 3821665 • Letter: T
Question
The following is the structure of process P_i from an algorithm attempting to solve the two-process critical section problem. while (1) {while (turn !=i); critical section turn = j remainder section} where turn is a shared integer variable, i is an integer zero or one and represents the index of Process P_i, j is an integer zero or one and represents the index of the other running Process P_j. (a) What is the problem with this algorithm? (b) How can it be fixed? (c) Write the structure of process P_i that solves the two-process critical section problem.Explanation / Answer
a) the main problem with this algiorithm is that it will not let the processes run synchronously and does not gurantee mutual exclusion i.e at a single point of time only one process will continue to perform the critical section. It will execute the critical section once for process Pi and will not let other process i.e. Pj execute its critical section.
b) It can be solved by changing the structure of the process Pi which is mentioned in the next part of the question.
c)
#include <stdio.h>
int turn = 0;
int main() {
int i = 0, j = 1;
while(1) {
while(turn != i) {
printf("Turn j "); //Critical Section of process Pj
sleep(1);
turn = 1 - j;
}
while(turn != j) {
printf("Turn i "); //Critical section of process Pi
sleep(1);
turn = 1 - i;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.