CS-230 Programming Assignment #1

Due date: Monday, November 1, 1993

1. Implement locks. You may either use semaphores as a building block, or you may use more primitive thread routines (such as Thread::Sleep). We have provided the public interface to locks and condition variables in synch.h. You need to define the private data and implement the interface.

A lock is a variable that handles the operations Acquire() and Release(). When lock-¿Acquire() is executed and the lock is free, the lock state is simply set to busy and the function returns. If the lock is busy, then the thread calling Acquire() blocks until the lock is free. When lock-¿Release() is called and the lock is free, the function simply returns. If the lock is busy then it is set to free and a process blocked on the lock is made ready. If the lock is busy and not held by the thread doing the Release(), the function simply returns.

2. Implement producer/consumer communication through a bounded buffer using locks. The bounded buffer should be an explicit data-structure (i.e., class) that handles the operations Put(char) and Take() (and any others you think necessary). Your solution should include no busy waiting.

The producer places characters from the string ”Hello world” into the buffer one character at a time using Put; it must wait if the buffer is full. The consumer pulls characters out of the buffer one at a time using Take and prints them to the screen; it must wait if the buffer is empty. Test your solution with a buffer that holds more than one character at a time, with long messages, and with multiple producers and consumers.