CSE150 Operating Systems (Spring semester 2008): threads and IPC
Consider the following programs (corresponding to figures in the textbook), which are implemented with threads (Pthread in Linux):
- fig2-15.c: creates N threads that run for a while (printing their id).
- strict_alt.c (fig. 2-23): creates 2 threads that run for a while (printing their id, computing, and entering/exiting a critical region). This uses the solution of strict alternation (and busy waiting).
- peterson.c (fig. 2-24): like fig. 2-23 but for Peterson's solution (busy waiting, no alternation necessary).
- fig2-32.c: producer-consumer problem with threads.
- ProducerConsumer.java (fig. 2-35): producer-consumer problem with Java synchronised methods ("monitors").
To compile and run these programs, do gcc -lpthread -o peterson peterson.c; ./peterson. They have internal parameters (e.g. the number of threads for fig2-15.c, or the "computation timeouts" for strict_alt.c and peterson.c. Make sure you understand what the programs do by reading their source. Then, compile and run the programs, and observe their output carefully (observe how fast the output appears and in what order). Some things to try:
- Run the same executable multiple times. Is the output the same every time? Do the threads execute always in the same order?
- Change the value of these parameters (e.g. make a certain timeout 10 times longer), recompile and run. How does the output change?
- The threads print their id in two different ways, using printf and using fprintf...fflush. What is the difference?
- Can you say something about how the OS schedules threads, based on the behaviour of these programs? Do threads run uninterrupted?
- Do race conditions occur for strict_alt.c or peterson.c? Do threads alternate entering the critical regions?
- ProducerConsumer.java: does the buffer stay within its limits (0, N)?
Run peterson, strict_alt and top in separate windows so you can monitor the status of these processes.
Miguel A. Carreira-Perpinan
Last modified: Tue Apr 1 00:29:38 PST 2008
UC Merced |
EECS |
MACP's Home Page