You are on page 1of 4

Producers and Consumers Problem

• A producer process generates information that is to be processed by the consumer


• 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

Solution
semaphore mutex = 1;
semaphore empty = BUFFER_SIZE;
semaphore full = 0;
 
producer() {
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
}
}
consumer() {
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

• 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

Solution:

semaphore mutex=1;
semaphore write=1;
int readcount = 0;

Writer Algo
P(write);
// Writing
V(write);
Reader Algo
P(mutex);
readcount + + ;
if readcount == 1 then P(write);
V(mutex);
//reading
P(mutex);
readcount - - ;
if readcount == 0 then V(write);
V(mutex)

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 chopsticks. There are only 5 chopsticks placed between
the philosophers
• A philo may use only the chopsticks placed on his left and right sides

Solution1: Deadlock

Semaphore Chopstick[4];//initialized to 1

Process Pi:
While(1){
think();
P( chopstick[i]);
P( chopstick[i+1 mod 5]);
eat();
V(chopstick[i+1 mod 5]);
V(chopstick[i]);
}

Solution2:

• The solution consists in limiting the eating philosophers to 4 in order to prevent


deadlock.

• We use a semaphore max4 initialized to 4

Semaphore max4=4;

Process Pi:
While(1){
think();
P(max4);
P( chopstick[i]);
P( chopstick[i+1 mod 5]);
eat();
V(chopstick[i+1 mod 5]);
V(chopstick[i]);
V(max4);
}

barber shop
In this problem we would like to simulate a barber shop using semaphores. The problem
is defined as follows:
 We have one barber and many customers.
 The shop capacity is defined by the number of chairs which determines the
maximum number of waiting customers.
 One customer is served at a time.
 A customer leaves if the shop is full (i.e. all chairs are occupied)
Hint: Use two semaphores: barber wait for customers; customers wait for barbers, and
use a variable waiting to count the waiting customers.
The barber job consists of :
 Looking if there is any waiting customer
 Decrementing the waiting variable
 Serving the customer
The customer’s job consists of:
 Looking if there is free chairs in the shop
 Incrementing waiting variable
 Waiting to be served
 Getting hair cut

Solution:

#define CHAIRS 5

semaphore customers = 0;
semaphore barbers = 0;
semaphore mutex = 1;
int waiting = 0;

barber()
{
while(TRUE) {
P(customers); /* block if no customers */
P(mutex); /* access to ‘waiting’ */
waiting = waiting - 1;
V(barbers); /* barber is in.. */
V(mutex); /* release ‘waiting’ */
cut_hair(); }
}

customer()
{
P(mutex); /* enter CS */
if(waiting < CHAIRS) {
waiting = waiting + 1; /* increment waiting */
V(customers); /* wake V barber */
V(mutex); /* release ‘waiting’ */
P(barbers); /* block for 0 barbers */
get_haircut();
}
else {
V(mutex); /* shop full .. leave */
}
}

You might also like