# Critical section problem

1. Feb 8, 2013

### whitehorsey

1. Is mutual exclusion, progress, and bounded waiting satisfied in this code?

Code: Two process solution to the critical section problem

For this problem, you can decide any case you want whether the processes go one after the other, execute two lines at once then switch to the other process, etc.

3. This is what I got:
Looking at this code it looks like it will never leave the while loop and hit the critical section so no it does not satisfy mutual exclusion.
Progress can only be made if you hit the critical section since it keeps going around in the while loop then no.
Lastly bounded waiting there is no time limit to enter the critical section? This one I'm not really sure about.

2. Feb 9, 2013

### rcgldr

If you assume that both processes execute in parallel, so that each one executes the same line number at the same time, then you're correct, the code will get stuck in an infinite loop.

Where did you get this example code from? There is code similar to this that will work properly, but I'm not sure if this is homework and you're supposed to fix the code.

3. Feb 9, 2013

### whitehorsey

I got this code while I was surfing the net for examples to practice. I have a test coming up =/
I knew this code would run infinitely but I also don't really understand it. For example, in the while statement (its parentheses) whenever while is whatever the flag is it will always, no matter what enter into that while loop block? If so, when will it ever hit lines 7, 9, 10, and 11?

4. Feb 9, 2013

### rcgldr

Take the case where they both run at the same time and the same line, both run line 0 at the same time, both run line 1 at the same time, ... , both run line 7 at the same time, line 8, and both loop back to line 2 at the same time.

You need at least one more variable or a processor with a special instruction that can load (or test) and store in a single atomic (not interruptable by another process) operation.

If only two processes are running at the same time, Dekker's or Peterson's (these are similar) algorithm can be used. These algorithms won't work on processors that change the order of insructions. Wiki links:

wiki_Dekkers_algorithm.htm

wiki_Petersons_algorithm.htm

Last edited: Feb 9, 2013
5. Feb 15, 2013

Thank You!!