46.4k views
4 votes
Is the following solution correct to solve the critical section problem? Please briefly explain.

proc(int i) {
while(TRUE) {
compute: while(flag[(i+1)%2]);
flag[i] = TRUE;
critical_section;
flag[i] = FALSE;
}
}

shared boolean flag[2];
flag[0] = flag[1] = FALSE;

forkproc,1.0
forkproc,1,1

User Kekert
by
8.4k points

1 Answer

2 votes

Final answer:

The provided solution is incorrect for solving the critical section problem as it is missing a turn variable and may allow both processes to enter the critical section simultaneously, breaking mutual exclusion.

Step-by-step explanation:

The solution provided to solve the critical section problem is based on Peterson's algorithm but has an error in its implementation. The correct algorithm uses two flag variables and one turn variable to enforce mutual exclusion. In the provided solution, the compute phase improperly uses the flag array which could lead to both processes entering the critical section at the same time, thus breaking the mutual exclusion property.

To solve the critical section problem, the algorithm should allow a process to enter its critical section if it is its turn or if the other process is not interested in entering the critical section. The code snippet provided is missing the control variable for indicating the process's turn which is crucial for the algorithm's correctness.

Therefore, as it stands, the given solution is not correct for solving the critical section problem as mutual exclusion may not be guaranteed.

User JacobJacox
by
7.7k points

Related questions