Operating Systems


Homework #3

Due: 2007/4/17


For the following programming exercises, please finish them by using C language and use the workstations at IM workstation laboratory to compile/execute them.


1.      The producer-consumer problem: The source code of a producer-consumer program implemented using shared-memory can be got via


Rewrite the program using Pthreads library, so that the producer and the consumer can be run as two different threads.


2.      The Fibonacci sequence problem: The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as:

fib0 = 0
fib1 = 1
fib2 = fibn-1 + fibn-2

Write a multithreaded program that generates the Fibonacci sequence using Pthreads library. This program should works as follow: The user will enter on the command line the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in data that is shared by the threads (an array is probably the most convenient data structure). When the thread finishes execution, the parent thread will output the sequence generated by the child thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finishes, this will require having the parent thread wait for the child thread to finish.


Submission & Grading


The deadline is 4/17. Please send your source code to one of the TAs by E-mail before 11:59PM of that day. The file name should be your student id like B91705056.rar. If you want to submit a newer edition of your code, please rename it like B91705056_1.rar. In the rar file, please include 1) the source codes, 2) an brief report about how you wrote your code, your personal understanding about the mechanism behind your work, and a series of test input/output.


Grading: Programs: 70%; Documentation: 30%.




a.       You can use any resources you found on the Internet, or discuss with others. However, you should not use any code or library which is not written by you, except for the system calls and build-in functions. Any kind of copying will result in zero points. (including the one who gsharedh his/her exercises)

b.      You can ask Robin or TAs if you have any question, but we will not help you to do the exercises anyway.

c.       Any exercise after the deadline is not acceptable.