[go: up one dir, main page]

0% found this document useful (0 votes)
31 views2 pages

Barbershop Problem Semaphore Solution

The document describes a simulation of a barbershop with a waiting room, barber chair, and customers. It includes shared data structures and code for the customer entry process and the barber process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Barbershop Problem Semaphore Solution

The document describes a simulation of a barbershop with a waiting room, barber chair, and customers. It includes shared data structures and code for the customer entry process and the barber process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Weekly 3 Assessment Questions

A barbershop consists of a waiting room with n chairs and a barber room with one barber
chair. If there are no customers to be served, the barber goes to sleep. If a customer enters
the barbershop and all chairs are occupied, then the customer leaves the shop. If the
barber is busy but chairs are available, then the customer sits in one of the free chairs. If
the barber is asleep, the customer wakes up the barber.
Write a program to coordinate the barber and the customers.
// shared data
semaphore waiting_room_mutex = 1;
semaphore barber_room_mutex = 1;
semaphore barber_chair_free = n;
semaphore sleepy_barbers = 0;
semaphore barber_chairs[n] = {0, 0, 0, …};
int barber_chair_states[n] = {0, 0, 0, …};
int num_waiting_chairs_free = N;

boolean customer_entry( ) {
// try to make it into waiting room
wait(waiting_room_mutex);
if (num_waiting_chairs_free == 0)
{ signal(waiting_room_mutex);
return false;
}

num_waiting_chairs_free--; // grabbed a chair


signal(waiting_room_mutex);

// now, wait until there is a barber chair free


wait(barber_chair_free);

// a barber chair is free, so release waiting room chair


wait(waiting_room_mutex); wait(barber_room_mutex);
num_waiting_chairs_free++;
signal(waiting_room_mutex);

// now grab a barber chair int


mychair;
for (int I=0; I<n; I++) {
if (barber_chair_states[I] == 0) { // 0 = empty chair
mychair = I;
break;
}
}
barber_chair_states[mychair] = 1; // 1 = haircut needed
signal(barber_room_mutex);

// now wake up barber, and sleep until haircut done


signal(sleepy_barbers); wait(barber_chairs[mychair]);

// great! haircut is done, let’s leave. barber


// has taken care of the barber_chair_states array.
signal(barber_chair_free);
return true;
}

void barber_enters() {
while(1) {
// wait for a customer
wait(sleepy_barbers);
// find the customer
wait(barber_room_mutex); int
mychair;
for (int I=0; I<n; I++) {
if (barber_chair_states[I] == 1) {
mychair = I;
break;
}
}
barber_chair_states[mychair] = 2; // 2 = cutting hair
signal(barber_room_mutex);

// CUT HAIR HERE


cut_hair(mychair);

// now wake up customer wait(barber_room_mutex);


barber_chair_states[mychair] = 0; // 0 = empty chair
signal(barber_chair[mychair]);
signal(barber_room_mutex);

// all done, we’ll loop and sleep again


}
}

You might also like