Professional Documents
Culture Documents
Process Communication and Synchronization
Process Communication and Synchronization
and Synchronization
Race Conditions
• A race condition is a situation where two
processes are reading or writing the same
location and the result depends on which goes
first
• Can happen when you have a shared database
with a preemptive scheduling algorithm (like
Round Robin).
• Lots of other resources are shared, too
• the programs enter sometimes critical sections
where they must use shared resources (they must
do this code as a unit)
Why Sharing?
• not allowing any sharing solves the problem
but not necessarily practical
– Processes have to share operating system and
hardware (printers, modems, etc…)
– May have many users of a database, applications,
…
Solution
• we need the operating system to share
resources and data among the processes
• Since most resources can’t be shared, we
must synchronize their use
• There are several common mechanisms for
doing this
Criteria For Solutions
0 1 2 3 4 5 6 7
A B C D E F
out in
Producers and Consumers Problem
-2-
semaphore mutex = 1;
semaphore empty = BUFFER_SIZE;
semaphore full = 0;
void producer(void) {
while(1) {
produce_item(); // generate next item
P(empty); // decrement empty count
P(mutex); // enter critical region
enter_item(); // put item in buffer
V(mutex); // leave critical region
V(full); // increment count of full
elements in buffer
}
}
Producers and Consumers Problem
-3-
void consumer(void) {
while(1) {
P(full); // decrement full count
P(mutex); // enter critical region
remove_item(); // remove item from buffer
V(mutex); // leave critical region
V(empty); // increment count of empty slots
consume_item(); // print item
}
}
Readers and Writers -1-
• Here a number of users want to read or write the
same section of a database for ex.
• Readers can read together while Writers can’t
• But Reader can’t read if nothing is written!!!
• Uses two semaphores, one for each class
• Both classes must check both semaphores
• Special mechanisms to prevent starvation
Readers and Writers -2-
Reader Algo
semaphore mutex=1; P(mutex);
semaphore write=1; readcount + + ;
int readcount = 0;
if readcount == 1
then P(write);
Writer Algo V(mutex);
P(write); //Reading
P(mutex);
// Writing
readcount - - ;
V(write); if readcount == 0
then V(write);
V(mutex):
Deadlock
• Deadlock occurs when processes cannot
proceed because each holds resources that
the other(s) needs for progress
• The problem results from poor
synchronization of processes and the
resources they need
Deadlock: Example
Process A Process B
HOLDS:
WANTS:
Resource 1 Resource 2
Four Conditions for Deadlock
1. Mutual exclusion (only one process can have
the resource)
2. Resource holding (resource can’t be shared
—true of most resources)
3. No preemption (a process holds the
resource until it is done with it)
4. Circular wait (processes are waiting for
other’s resources in circular fashion)
Circular Wait in Deadlock
holds
wants
R1
Process A Process B
R2
R3
Process C
Starvation
• Deadlock occurs because we give away so
many resources that processes can’t
complete because they need each other’s
resources
• Starvation is the opposite effect: some
processes (usually those requiring lots of
resources) never get the chance to run
• Can easily happen to large jobs that require
a great many resources to run
A Classical Example: The Dining
Philosophers
• 5 philosophers are sitting
on a round table for
dinner (noodles). They eat
and think.
• To eat a philo must use 2
spaghetti
chopsticks. There are only
5 chopsticks placed
between the philosophers
• A philo may use only the
chopsticks placed on his
left and right sides
Proposed Solution
Semaphore
Chopstick[4];//initialized
to 1
Process Pi: Deadlock
While(1){
think();
P( chopstick[i]);
P( chopstick[i+1 mod 5]); If All try to get left
eat(); chopstick together
V(chopstick[i+1 mod 5]);
V(chopstick[i]);
}
Better Solution
• The solution consists in Process Pi:
limiting the eating While(1){
philosophers to 4 in order think();
to prevent deadlock.
P(max4);
P( chopstick[i]);
• We use a semaphore max4
initialized to 4 P( chopstick[i+1 mod 5]);
eat();
Semaphore max4=4; V(chopstick[i+1 mod 5]);
V(chopstick[i]);
V(max4);
}
End