Homework 9 - Concurrency in Ruby

Due Wednesday April 27


Write a Producer-Consumer problem in Ruby. Use a bounded buffer similar to the examples given in class (or in the text book).

Use classes for the buffer, producer, and consumer.

Bounded Buffer: Use a parameter to initialize the size of the buffer when it is constructed. Use semaphores, monitors, and/or condition variables as appropriate to control access to the buffer.

Producer: Use a parameter to initialize a name for the producer when it is constructed. Use random values as the inputs to be stored in the buffer. Have the producer threads sleep for a random time after each value is inserted.

Consumer: Use a parameter to initialize a name for the consumer when it is constructed. Have the consumer threads sleep for a random time after each value is removed.

Create several producer and consumer threads when running the program. Experiment with different numbers of producers and consumers and different sleep times to see how the program performs with various configurations.

Output

  1. Each time an item is inserted into the buffer, print the name of the producer, position where stored, and the value inserted.

  2. Each time an item is removed from the buffer, print the name of the consumer, position where stored, and the value inserted.

  3. Print a message whenever a producer has to wait to insert a value because the buffer is full - include the name of the producer in the output.

  4. Print a message whenever a consumer has to wait to remove a value because the buffer is empty - include the name of the consumer in the output.

Extra: Include a timer to allow the program to execute for a fixed amount of time.