Professional Documents
Culture Documents
Operating
Systems
Outline
Introduction
Synchronization Problems
Synchronization Solutions
Operating
Systems
Cooperating Processes
Shared memory
Shared file
Mailboxes or pipes
Operating
Systems
Race condition
Operating
Systems
Race Condition
Operating
Systems
Race Condition
Operating
Systems
Race Condition
As the CPU can only run one process at a time, consider the
following instruction execution when context switches occur
in a particular pattern.
If the context switch does not occur here, then the program
would run correctly.
However, as there is no process control when the context
switch takes place, then there is a chance the system can
end up in an inconsistent state
Operating
Systems
Race Condition
Beerdrinker: top = top 1
Bartender: beerstack[top] = new Beer // this would place a beer on top of an existing beer!
Bartender: top = top + 1
Beerdrinker: drink beerstack[top] // beerdrinker would drink an empty mug!
If the context switch does not occur here, then the program
would run correctly.
However, as there is no process control when the context
switch takes place, then there is a chance the system can
end up in an inconsistent state.
There is therefore a need for a process to determine a
critical section of code where no context switch may occur.
Operating
Systems
Resource Competition
Operating
Systems
Allows processes to wait for resources and notify others when they
are made available
Operating
Systems
10
Operating
Systems
11
Operating
Systems
12
Operating
Systems
13
Mutual exclusion
Progress
Bounded wait
Operating
Systems
14
Mutual Exclusion
Operating
Systems
15
Progress
If there are more than one process waiting to enter its critical
section...
Operating
Systems
16
Bounded Wait
A process cannot be
allowed to wait forever to
enter its critical section
Operating
Systems
17
Critical Section
Simply have the manipulation of the top variable and the addition (or
deletion) of a stack object be in a critical section.
Operating
Systems
18
Operating
Systems
19
Dining Philosophers
Operating
Systems
20
Dining Philosophers
Operating
Systems
21
Busy Wait
Semaphores
Monitors
Operating
Systems
22
Busy Wait
Operating
Systems
23
Busy Wait
The code is for process i. Process j code is simply the inverse of this
Operating
Systems
24
Busy Wait
do {
yet
while (turn!=i) { }
criticalsection();
turn = j;
remaindersection();
} while (true);
Operating
Systems
25
Busy Wait
If process i loops very fast, it would wait at the busy loop until
process j finishes its remainder section and enter its critical section
again
Operating
Systems
26
Operating
Systems
27
if (turn != i) {
wait(); // stop running until i am notified by j
}
criticalsection();
turn = j;
//after i is done done, its j's turn
notify(j);
// wakeup j if it is waiting.
remaindersection();
} while (true);
Operating
Systems
28
Semaphores
Operating
Systems
29
Semaphores
class Semaphore {
int ctr = 1;
ProcessList L;
void wait() {
ctr = ctr 1;
if (ctr < 0) {
add this process to L;
block;
}
}
void notify() {
ctr = ctr + 1;
if (ctr <= 0) {
get a process from L and resume its
running
}
}
}
Operating
Systems
30
Semaphores
Operating
Systems
31
Semaphore
class Semaphore {
int ctr = 1;
ProcessList L;
void wait() {
ctr = ctr 1;
if (ctr < 0) {
add this process
to L;
block;
}
}
void notify() {
ctr = ctr + 1;
if (ctr <= 0) {
get a process
from L
and resume its
running
}
}
}
Operating
Systems
Semaphore
class Semaphore {
int ctr = 1;
ProcessList L;
void wait() {
ctr = ctr 1;
if (ctr < 0) {
add this process
to L;
block;
}
}
void notify() {
ctr = ctr + 1;
if (ctr <= 0) {
get a process
from L
and resume its
running
}
}
}
Operating
Systems
33
Semaphores
Operating
Systems
34
Dining Philosopers
do {
i.e. What happens when everyone grabs their left chopstick all at
once?
Operating
Systems
35
Monitors
Operating
Systems
36
Monitors
Operating
Systems
37
Exercises
Give examples of problems (situational) in process
synchronization (at least 3 problems).
Provide the solutions for your given problems based from the
stated process synchronization solutions.
Operating
Systems
38