


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
4 algorithms for the critical section problem
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Algorithm #1 (Incorrect)
One approach to solving the problem might be to have the processes take turns accessing the resource. Consider the following proposed solution for a two-process problem:
/* i is this process; j is the other process */
while (true) { while (turn != i); /* spin until it’s my turn */
<<< critical section >>>
turn = j;
<<< code outside critical section >>> }
To understand why this code is incorrect, we must consider the characteristics of a solution that we discussed earlier. This solution does ensure mutual exclusion, but it is not correct. The proposed solution violates both the progress criteria and the bounded wait criteria:
Observations of Algorithm #
Let's Try Again - Algorithm #2 (Incorrect)
Let's put what we learned in Algorithm #1 to work and try again. This time, we won't worry about turns. We'll juggle if the other process wants a turn. If it is not looking for the critical section, we won't have to forego its use. /* i is this process; j is the other process */
while (true) { while (state[j] == inside); /* is the other one inside? */
state[i] = inside; /* get in and flip state */
<<< critical section >>>
state[i] = outside; /* revert state */
<<< code outside critical section >>> }
This proposal has some nice features:
But we still don't have a solution. To understand why this code is incorrect, we must remember two things:
Atomicity is the property of being executed as a single unit. This algorithm assumes that the test of (state[1] == inside) and the set of (state[0] = inside) are atomic. That is to say, this algorithm assumes that nothing can come in-between those two operations.
That assumption is inaccurate. A race-condition exists between testing and setting state. P 0 can be pre-empted between the two operations, by P 1. The result will be that P 1 will test state[0], find it false, and enter the critical section.
Both P 0 and P^1 loop forever. This is the livelock.
Algorithm #4: Peterson's Algorithm (Correct)
This time, let's try using Algorithm #3, but taking turns to break ties: /* i is this process; j is the other process */
while (true) { state[i] = interested; /* declare interest / turn = j; / be nice to other guy */
while (state[j] == interested && turn == j);
<<< critical section >>>
state[i] = notinterested; /* we’re done */
<<< code outside critical section >>> }
This code satisfies all three properties:
It is interesting to note that this requires a formal proof to be really convincing. Proving erroneousness is easy: just give a counter-example. But proving correctness is much more difficult, since no number of examples is convincing. Instead, we'll focus on justifying the correctness with the same level of rigor that you might you during a discussion with your project partner.