Semaphores
A Semaphore is a non-negative integer variable that can be used as a binary signal, a flag.In operating system , a semaphore is set either 0 or 1 to perform a similar function. It signals if and when a resource is free and can be used by a process.
Laundromat
- A Laundromat has 30 washing machines and 15 dryers. Assuming that all customers use one washing machine to wash their clothes then one dryer to dry them, add the necessary semaphore calls to the following program segment.
semaphore _washers = 30;
semaphore _dryers = 15;
customer (int who) {
_P(&washer);
wash_clothes();
_V(&washer);
_P(&dryer);
dry_clothes();
V(&dryer);
}
The ice-cream parlor
- An ice-cream parlor has two employees selling ice cream and six seats for its customers. Each employee can only serve one customer at a time and each seat can only accommodate one customer at a time. Add the required semaphores to the following program skeleton to guarantee that customers will never have to wait for a chair with a melting ice-cream in their hand.
semaphore _seats = 6;
semaphore _employees = 2;
customer (int who) {
P(&seats); P(&employees);
order_ice_cream();
V(&employees);
eat_it();
V(&seat);
} // customer
The pizza oven
- A pizza oven can contain nine pizzas but the oven narrow opening allows only one cook at a time to either put a pizza in the oven or to take one out. Given that there will be more than one cook preparing pizzas at any given time, complete the missing lines in the following C procedure.
semaphore oven = 9;
semaphore access = __ 1__; // the mutex
make_pizza(int size, int toppings) {
prepare_pizza(size, toppings);
P(&oven); P(&access);
put_into_oven(); V(&access);
wait_until_done();
P(&access);
take_from_oven();
V(&oven); V(&access); // IN ANY ORDER!
} // make_pizza
The Bus
- Assume a bus has 30 passengers capacity , and has two doors. Add semaphores to the following two functions to ensure that(a) the bus will never be overloaded and (b) passengers will not collide with each other when getting on or getting off the bus.
semaphore bus = 30;
semaphore door = 2;
getting_on(){
P(&bus);
P(&door);
walk_in();
V(&door);
}//getting on
getting_off(){
P(&door);
walk_out();
V(&door);
V(&bus)
}// getting off
Customer & Producer
- The classic problems of producers (such as CPUs)and consumers(such as a printers) concerns one or more process data that one or more process consumes later through a single buffer. Systems must make sure that the producer won’t try to add data to full a buffer, and the consumer won’t try to make withdrawals from an empty buffer. And for the integrity of data only one process must be allowed to access the buffer at a time. Assume buffer contain 5 files maximum, design the procedures and consumers processes using semaphores and mutex.
semaphore mutex = 1; /* for mutual exclusion*/
semaphore empty = N; /* number empty buf entries */
semaphore full = 0; /* number full buf entries */
Producer
do {
wait(empty);
wait(mutex);
//produce item
//update “In”
signal(mutex);
signal(full);
} while (true);
Consumer
do {
wait(full);
wait(mutex);
//consume item
//update “Out”
signal(mutex);
signal(empty);
} while (true);
or
Shared pointers: “In”, “Out”
Shared Semaphores: mutex, empty, full;
mutex = 1; /* for mutual exclusion*/
empty = N; /* number empty buf entries */
full = 0; /* number full buf entries */
Producer
do {
wait(empty);
//produce item
//update “In”
signal(full);
} while (true);
Consumer
do {
wait(full);
//consume item
//update “Out”
signal(empty);
} while (true);