2. *Property of STI J0024
Race conditions usually occur if two or more processes are allowed to modify
the same shared variable at the same time.
To prevent race conditions, the operating system must perform process
synchronization to guarantee that only one process is updating a shared variable
at any one time.
3. *Property of STI J0024
part of the process that contains the instruction or instructions that access a
shared variable or resource
Process P1
.
.
.
mov ax, counter
inc ax
mov counter, ax
.
.
.
Process P2
.
.
.
mov ax, counter
inc ax
mov counter, ax
.
.
.
Critical
Section of
Process
P1
Critical
Section of
Process
P2
Figure 6.1 Critical Sections for Process P1 and P2
4. *Property of STI J0024
Software solutions to the critical section problem:
SOLUTION 1
Process P0
.
.
.
while (TurnToEnter != 0) { };
TurnToEnter = 1;
.
.
.
critical section
Process P1
.
.
.
while (TurnToEnter != 1) { };
TurnToEnter = 0;
.
.
.
critical section
Figure 6.2 Critical Sections of Processes P1 and P2 for Solution 1
5. *Property of STI J0024
SOLUTION 2
Process P0
.
.
.
WantToEnter[0] = true;
while (WantToEnter[1] == true) { };
WantToEnter[0] = false;
.
.
.
critical section
Process P1
.
.
.
WantToEnter[1] = true;
while (WantToEnter[0] == true) { };
WantToEnter[1] = false;
.
.
.
critical section
Figure 6.3 Critical Section P1 and P2 for Solution 2
6. *Property of STI J0024
SOLUTION 3 (Peterson’s Algorithm)
Process P0
.
.
.
WantToEnter[0] = true;
TurnToEnter = 1;
while (WantToEnter[1] == true && TurnToEnter == 1) { };
WantToEnter[0] = false;
.
.
.
critical section
Process P1
.
.
.
WantToEnter[1] = true;
TurnToEnter = 0;
while (WantToEnter[0] == true && TurnToEnter == 0) { };
WantToEnter[1] = false;
.
.
.
critical section
Figure 6.4 Critical Sections of Processes P1 and P2 for Solution 3
7. *Property of STI J0024
SOLUTION TO THE CRITICAL SECTION PROBLEM INVOLVING SEVERAL
PROCESS ( Bakery Algorithm)
Process Pi
.
.
.
choosing[i] = true;
number[i] = max(number[0], ... , number[n – 1]) + 1;
choosing[i] = false;
for (j = 1; j < n; j++) {
while (choosing[j] == true) { }
while ((number[j] != 0) &&
((number[j] < number[i]) || (number[j] == number[i]) && (j < i))) { };
}
number[i] = 0;
.
.
.
critical section
Figure 6.5 Critical Section of Process P1 for the Bakery Algorithm
8. *Property of STI J0024
Hardware solutions to the critical section problem:
Disabling interrupts
Special hardware instructions
boolean test_and_set (boolean &i)
{
boolean val;
if (*i == false) {
val = false;
*i = true;
} else
val = true;
return val;
}
These instructions are
executed atomically
(uninterruptible)
Figure 6.6 Definition of test_and set instruction
9. *Property of STI J0024
.
.
.
while (test_and_set (&lock) == true) { };
*lock := false;
.
.
.
critical section
Process Pi
Figure 6.7 Using the test_and_set instruction to solve
the critical section problem
10. *Property of STI J0024
Semaphore is a tool that can easily be used to solve more complex synchronization
problems and does not use busy waiting.
.
.
.
wait(mutex);
signal(mutex);
.
.
.
critical section
Process Pi
Figure 6.8 Using Semaphores to Solve the
Critical Section Problem
11. *Property of STI J0024
The Dining Philosophers Problem
Restrictions:
1. A philosopher cannot start eating unless he has both forks.
2. A philosopher cannot pick up both forks at the same time. He has to do it one
at a time.
3. He cannot get the fork that is being used by the philosopher to his right or to
his left.
Figure 6.9 The Dining Philosophers Problem
12. *Property of STI J0024
.
.
.
wait(LeftFork);
wait(RightFork);
eat;
signal(RightFork);
signal(LeftFork);
.
.
.
Figure 6.10 Solution to the Dining Philosophers Problem
13. *Property of STI J0024
The Readers and Writers Problem
.
.
.
wait(wrt);
signal(wrt);
.
.
.
start writing data
Writer Process
Figure 6.11 Using Semaphores to Implement a Writer Process
14. *Property of STI J0024
.
.
.
wait(mutex);
readcount++;
if readcount ==1
wait(wrt);
signal(mutex);
wait(mutex);
readcount--;
If readcount == 0
signal(wrt);
signal(mutex);
.
.
.
start reading data
Reader Process
Figure 6.12 Using Semaphores to Implement a Reader Process