Previous Contents Next

Exercises

The Philosophers Disentangled

To solve the possible deadlock of the dining philosophers, it suffices to limit access to the table to four at once. Implement this solution.

More of the Post Office

We suggest the following modification to the post office described on page ??: some impatient clients may leave before there number has been called.
  1. Add a method wait (with type int -> unit) to the class dispenser which causes the caller to wait while the last number distributed is less than or equal to the parameter of the method (it is necessary to modify take so that it emits a signal).
  2. Modify the method await_arrival of class counter, so that it returns the boolean value true if the expected client arrives, and false if the client has not arrived at the end of a certain time.
  3. Modify the class announcer by passing it a number dispenser as a parameter and:
    1. adding a method wait_until which returns true if the expected number has been called during a given waiting period, and false otherwise;
    2. modifying the method call to take a counter as parameter and update the field nclient of this counter (it is necessary to add an update method in the counter class).


  4. Modify the function clerk to take fruitless waits into account.
  5. Write a function impatient_client which simulates the behaviour of an impatient client.

Object Producers and Consumers

This exercise revisits the producer-consumer algorithm with the following variation: the storage warehouse is of finite size (i.e. a table rather than a list managed as a FIFO). Also, we propose to make an implementation that uses objects to model resources, like the post office.

  1. Define a class product with signature:
     
    class product : string ->
    object
    val name : string
    method name : string
    end
  2. Define a class shop such that:
     
    class show : int ->
    object
    val mutable buffer : product array
    val c : Condition.t
    val mutable ic : int
    val mutable ip : int
    val m : Mutex.t
    val mutable np : int
    val size : int
    method dispose : product -> unit
    method acquire : unit -> product
    end
    The indexes ic and ip are manipulated by the producers and the consumers, respectively. The index ic holds the index of the last product taken and ip that of the last product stored. The counter np gives the number of products in stock. Mutual exclusion and control of the waiting of producers and consumers will be managed by the methods of this class.

  3. Define a function consumer: shop -> string -> unit.
  4. Define a function create_product of type string -> product. The name given to a product will be composed of the string passed as an argument concatenated with a product number incremented at every invocation of the function.
    Use this function to define producer: shop -> string -> unit.

Previous Contents Next