Homework 2


Out: Friday, April 10th

Due: Friday, April 17th, *BEFORE CLASS STARTS* Turn In: https://catalysttools.washington.edu/collectit/dropbox/andosw/5665

Read the text: Silberschatz Chapters 4-5

Problems in text:

3.7, 4.2, 4.3, 4.8, 4.11 (assuming a single kernel thread is powering all of the user-level threads), 5.2, 5.3, 5.5, 5.15

4.x For this question you are to use the pthread library to write a simple producer/consumer style program. Your program should create two threads: one to produce data and one to consume data. The data that you are producing and consuming are just random integers between 0 and 99. Your code should implement the following pseudo code: main: initialize buffer to store data. The buffer needs to store 32 ints. initialize any mutex/condition variables create/start threads join threads free buffer/memory return producing thread: Loop 100 times If buffer is full print "buffer full, waiting..." wait for buffer to not be full endif generate random int between 0 and 99 and add it to the buffer print "produced x" where x is the int produced sleep either 1 second or 4 seconds. 50% chance for each endloop consuming thread: Loop 100 times If buffer is empty print "buffer empty, waiting..." wait for buffer not to be empty endif consume an int print "consumed x" where x is the int consumed sleep either 1 second or 5 seconds. 50% chance for each endloop *If the buffer is ever full, the producing thread does not add to it. *If the buffer is ever empty, the consuming thread does not consume anything. *It might help to think of your buffer as a circular array. *Make sure your buffer is coherent. ie. use a mutex! *When waiting, use the pthread_cond_wait() function *Make sure you signal when the buffer is not full or not empty with pthread_cond_signal() *Make sure you compile with the -pthread flag