Critical Section: A section of code which must be executed
mutually exclusively in time, such as the modification of a shared variable.
entry section: The code which requests permission to enter the
critical section.
critical section: as above
exit section: The code which removes the mutual exclusion.
remainder section: Everything else
Solutions to the critical section problem must satisfy the following:
Mutual Exclusion: At most one process is in its critical
section at any time.
Progress
If all other processes are in their remainder sections,
a process that wishes to enter its critical section can get in.
Only those processes that are not in their remainder section
can participate in the decision as to which process will enter its
critical section next, and this decision cannot be postponed indefinitely.
Bounded Waiting:
There must be a bound on the number of times that other processes are
allowed to enter their critical sections after a process has made a request
to enter its critical section.
Bounded Waiting Definition
Here is an algorithm that does not solve the critical section problem:
Assume that turn is a shared vairable, initialized to 0 or 1.
Process 0: Process 1:
--------- ---------
while(TRUE) { while(TRUE) {
while (turn != 0) ; while (turn != 1) ;
critical secitoncritical section
turn = 1; turn = 0;
remainder sectionremainder section
} }
Bad Critical Section Problem Solution
Here is a correct solution known as Peterson's solution.
It uses three shared integer variables,
turn and flag[2].
Process 0 loop: Process 1 loop:
-------------- --------------
flag[0] = 1; flag[1] = 1;
turn = 1; turn = 0;
while (flag[1]==1 && turn==1); while (flag[0]==1 && turn==0);
critical sectioncritical section
flag[0] = 0; flag[1] = 0;
remainder sectionremainder section
Peterson's Solution Mutual Exclusion
will interchange the first two lines of Process 0.
Next Notes