Consider the following ‘busy waiting’ attempt for a mutual exclusion algorithm. Explain why this does not guarantee mutual exclusion. I can't figure out why it doesn't, to me it looks like it should can anyone explain why it doesn't?
/*Process0 */
while (flag[1]) do {/* nothing */}
flag[0] = true;
/*critical section */
flag[0] = false;
/*Process1 */
while (flag[0]) do {/* nothing */}
flag[1] = true;
/*critical section */
flag[1] = false;
It doesn't work because things you're apparently thinking of as atomic (i.e., inseperable) aren't anything of the sort.
Right now, you seem to assume that as soon as the
while
loop in one process exits, execution will continue through at least the entirety of the assignment toflag[N]
without any context switches.This simply isn't true. For the sake of argument, let's break each process into part A (the while loop) and B (the critical section). It's entirely possible to have a sequence like:
This is exactly the sort of code that works 99.9% of the time. You can probably test it like crazy for months on end, and it'll seem like it works perfectly--then you demo it in front of a customer, it crashes, burns, and destroys its surroundings. Then you send it back to the lab, and try though they will, they can't duplicate the problem.