Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

Process Communication

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

1. No processes simultaneously in critical


sections
2. No process outside its critical section can
block another process
3. No process should have to wait forever
(starvation)
Locking Mechanism
• Test-and-Set Lock (or TSL for short)
– Indivisible instruction that allows one process to
lock a resource—lock (L) reset to zero on exit
– Other processes needing that resource busy-wait
– To free the resource, unlock (L)
Semaphores
• Semaphores
– A more general mechanism for synchronization
– An integer variable is used to indicate how many
processes could access the resource simultaneously
• Two operations:
– Value of integer variable starts at 1
– When the resource is needed, a P or Wait operation
does the test and decrement as a single operation
– When a resource is freed, an increment and store is
done V or Signal as a single operation; then the
resource is available to other processes
Meaning of the Semaphore Variable -1-

• Say we have Semaphore S initialized to n


• If n is positive, it means n different processes can
access the critical section (or can proceed)
• Examples:
– Mutex is initialized to 1 meaning only one process can
execute P(mutex) and proceed (enter the critical
section)
– Synch Semaphore is initialized to 0 therefore any
process that executes P(Synch) is blocked until it is
waken up by some other process that executes
V(Synch)
Meaning of the Semaphore Variable -2-

• If S is negative or zero, it means there are


abs(S) different processes that are waiting
in the Semaphore queue to be waken up
• Example:
– When process B executes P(mutex), mutex
becomes -1, that means there is one process
blocked on mutex (waiting to be waken up,
which is done by V(mutex))
– Similar for the Synch semaphore.
Producers and Consumers Problem
-1-
• 2 kinds of processes:
– Producers: can’t produce more than N products (no place
for more than N objects).
– Consumers: can’t consume if there is no products
• In this problem, a buffer is being managed in which
the proc. Will read (consume) or write (produce)
• 2 semaphores are needed: one indicating full buffer
and one indicating empty buffer
Producers and Consumers Problem
-1-
• A producer process generates information that is to be processed by
the consumer Producers and Consumers Problem
-1- process
• The processes can run concurrently through the use of a buffer
• The consumer must wait on an empty buffer
• The producer must wait on a full buffer

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
  

You might also like