In addition to the main thread, create two additional threads:
Use shared variables as the means of communication between the threads. (Hint: You don't need pipe.) Also, since the two threads are started at the same time, and the second needs to know when the data from the first is available, some form of synchronization is required. You may do this in one of two ways (your choice):
pthread_join
on the first
thread before you start the second thread running.
Both your implementations should consist of a control program that (i) initializes the buffer and the synchronization variables and (ii) creates and terminates the threads for the producer and the consumer. You only need to have a single producer and a single consumer, but you should code with locks in place so that multiple of each would not cause problems.
The producer generates a stream of printable characters (either fixed or random) and places them into the buffer; and the consumer pulls the characters out of the buffer one at a time and prints them out. Let the buffer be of size 50. For the general test case, let the producer generate 500 characters and then quit, and devise some way of having the consumer quit once it has consumed the last character. While testing, it's probably useful for to also have the producer print them out, so that you can verify what's happenning.
Implement this using two different methods:
pthread_mutex_t
and sem_t
. Your code should simply realize
the pseudocode from figure 5.17 (page 218) of Stallings.
Use pthread_mutex_t
for mutual exclusion (from the pthread
library) and sem_t
for minding the buffer size (from the
posix4 library).