[UNB Logo]

CS2403: Operating Systems Principles

Assignment #3

Due: Monday, July 31 @ 2:45 p.m.
(Drop in the CS2403 assignment bin before class.)

Assignment Questions

1. The Sleeping-Barber Problem: A barber shop has one barber, one barber chair, and n chairs for waiting customers (if there are any) to sit in. If there are no customers present, the barber sits down in the barber chair and falls asleep. When a customer arrives, he has to wake up the sleeping barber. If additional customers arrive while the barber is cutting a customer's hair, they either sit down (if there are empty chairs) or leave the shop (if all chairs are full). The problem is to program the actions of the barber and his customers without getting into race conditions.

Pseudo code to solve this problem is shown below. However, all calls to "wait" and "signal" are missing. Your task is to add the missing "wait" and "signal" operations. Note: for each operation, make sure that you indicate which semaphore the operation is being performed upon - for example, for a wait operation specify wait(customers) OR wait(barber) OR wait(mutex).

Here is the pseudo code:

Shared data:

var customers: semaphore; // # of customers waiting for service
var barbers: semaphore; // # of barbers waiting for customers
var mutex: semaphore; // for mutual exclusion
var waiting: int; // customers waiting (not being cut)
var chairs: int; // # of chairs for waiting

customers:= 0;
barbers:= 0;
mutex:= 1;
waiting:= 0;
chairs:= 5;


Barber:

repeat
waiting:= waiting - 1; // decrement count of waiting customers
cut_hair(); // cut the customer's hair
until false;


Customer:

if (waiting < chairs)
then begin
waiting:= waiting + 1;
get_haircut(); // get a haircut
end
else


  • NOTE: A textual version of this pseudo code is available here.



  • 2. Write a monitor that implements an alarm clock that enables a calling program to delay itself for a specified number of time units (ticks). You may assume the existence of a real hardware clock, which invokes a procedure tick in your monitor at regular intervals.

    NOTE: Pseudo code is all that is required here (preferrably in the format that our textbook uses for pseudo code).

    This document can be found at http://www.cs.unb.ca/~nwebber/CS2403/assignments/assignment3.html
    Last updated July 27, 2000 by Natalie Webber.
    Contact Natalie at nwebber@unb.ca