Operating Systems 2026F: Tutorial 6: Difference between revisions
| Line 28: | Line 28: | ||
<li>Replace the call to srandom(time(NULL)) on line 169 with srandom(42). What differences do you notice in 3000pc-fifo's behavior?</li> | <li>Replace the call to srandom(time(NULL)) on line 169 with srandom(42). What differences do you notice in 3000pc-fifo's behavior?</li> | ||
<li>What descriptors are opened by 3000pc-fifo? What are they for and what files do the point to? How did you find this information? Hint: Try checking in /proc.</li> | <li>What descriptors are opened by 3000pc-fifo? What are they for and what files do the point to? How did you find this information? Hint: Try checking in /proc.</li> | ||
<li>Note that 3000pc-fifo-random uses /dev/urandom to pick its words instead of the C library's random functions. How do these two methods differ? You may wish to look at man urandom(4).</li> | |||
<li>What do you think would happen if you had multiple producers and multiple consumers in 3000pc-fifo? Would things break or work normally?</li> | <li>What do you think would happen if you had multiple producers and multiple consumers in 3000pc-fifo? Would things break or work normally?</li> | ||
<li>(Advanced) Implement multiple producers and consumers in 3000pc-fifo. Compare your results with your answer to question 4.</li> | <li>(Advanced) Implement multiple producers and consumers in 3000pc-fifo. Compare your results with your answer to question 4.</li> | ||
Revision as of 15:38, 8 November 2026
In this tutorial you'll be learning about two implementations of the producer-consumer problem, a classic example of a concurrency problem.
The Operating System Concepts textbook covers concurrency, threads, synchronization, and deadlocks in Chapters 4, 6, 7, and 8. OS:TEP textbook covers concurrency in great detail in Chapters 25-34, and the producer-consumer problem is covered in Chapter 30 (Condition Variables) and Chapter 31 (Semaphores). These textbook chapters cover much more material than we are covering in class; however, reviewing this material will give you useful context and thus is recommended.
A: Getting Started
- Make sure you system has the a recent version of bpftrace installed. The class VM has this already installed; on other systems, you'll have to install it yourself.
- Download the 3000pc archive (.tar.gz or .zip), unpack, and run make to compile 3000pc-fifo, 3000pc-fifo-random, 3000pc-rendezvous, and 3000pc-rendezvous-timeout.
- Note that these programs take 3 arguments each:
- The number of events to process
- The number of events to produce before the producer sleeps for 1 second
- The number of events to consume before the consumer sleeps for 1 second
- Run all four programs with the same arguments of your choice. Do this a few times. Do they behave the same way? Do you notice any differences?
- Repeat the above experiment, this time running each program under strace (with the -f flag to trace children too). Do you notice any difference in the system calls each program makes?
- Run sudo ./follow.bt and observe its output. What does it tell you about the executing programs? Does it output the same things for all of the programs?
B: Producer/Consumer with Pipes
- Examine the source code of 3000pc-fifo. Explain the following:
- What does the call to pipe(pipefd) on line 192 do? Hint: Look at the manpage for pipe(2)
- How does the consumer receive words to consume?
- How does the producer send words to the consumer?
- Why is the call to srandom(time(NULL)) on line 169 necessary?
- Replace the call to srandom(time(NULL)) on line 169 with srandom(42). What differences do you notice in 3000pc-fifo's behavior?
- What descriptors are opened by 3000pc-fifo? What are they for and what files do the point to? How did you find this information? Hint: Try checking in /proc.
- Note that 3000pc-fifo-random uses /dev/urandom to pick its words instead of the C library's random functions. How do these two methods differ? You may wish to look at man urandom(4).
- What do you think would happen if you had multiple producers and multiple consumers in 3000pc-fifo? Would things break or work normally?
- (Advanced) Implement multiple producers and consumers in 3000pc-fifo. Compare your results with your answer to question 4.
- Examine the source code of 3000pc-rendezvous. Explain the following:
- What are a few ways that 3000pc-rendezvous is different from 3000pc-fifo?
- What does the call to mmap on line 335 do? How did you figure this out?
- How does the producer notify the consumer that the queue is no longer empty?
- How does the consumer notify the producer that the queue is no longer full?
- What arguments can you provide to make the producer wait for the consumer? Hint: Check the size of the queue.
- What arguments can you provide to make the consumer wait for the producer?
- Another student tells you that the difference between processes and threads is that processes never share memory, while threads do. Is this statement correct or incorrect? How can the behavior of 3000pc-rendezvous help justify your answer?
- Change the calls to pthread_mutexattr_setpshared and pthread_condattr_setpshared to take 0 instead of 1. How does the behavior or 3000pc-rendezvous change? Does anything break? Demonstrate this using your answers to question 2 and question 3.
- 3000pc-rendezvous can deadlock when run for long enough, with no sleeps and with output not going to the terminal. 3000pc-rendezvous-timeout should not deadlock, but it may pause sometimes. What is the difference between these two programs? Why would this difference affect their tendency to deadlock?
- Benchmark the programs using the time command. For example, run 3000fifo as follows:
time (./3000pc-fifo 100000 0 0 > fifo-output.log)
Which one is fastest? Why? - How does replacing the pick_word() function in 3000pc-rendevous and 3000pc-rendevous-timeout with the one from 3000pc-fifo-random change their functionality? Do they run faster or slower? Why?
- (Advanced) Implement multiple producers and consumers in 3000pc-rendezvous. Compare your results to what you did in 3000pc-fifo.