


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CMPSCI 377 Operating Systems Fall 2008
Lecturer: Prashant Shenoy Scribe: Shashi Singh
As we already know, threads must ensure consistency; otherwise, race conditions (non-deterministic results) might happen.
Now consider the ātoo much milk problemā: two people share the same fridge and must guarantee that thereās always milk, but not too much milk. How to solve it? First, we consider some important concepts and their definitions:
For the above mentioned problem, we want to ensure some correctness properties. First, we want to guarantee that only one person buys milk when it is neeed (this is the safety property, aka ānothing bad happensā). Also, we want to ensure that someone does buy milk when needed (the progress property, aka āsomething good eventually happensā). Now consider that we can use the following atomic operations when writing the code for the problem:
Our first try could be to use the following code on both threads:
if (no milk and no note) leave note buy milk remove note
Unfortunately, this doesnāt work because both threads could simultaneously verify that thereās no note and no milk, and then both would simultaneously leave a note, and buy more milk. The problem in this case is that we end up with too much milk (safety property not met).
Now consider our solution #2:
Thread A:
8-2 Lecture 8: September 25
leave note "A" if (no note "B") if (no milk) buy milk remove note "A"
Thread B:
leave note "B" if (no note "A") if (no milk) buy milk remove note "B"
The problem now is that if both threads leave notes at the same time, neither will ever do anything. Then, we end up with no milk at all, which means that the progress property not met. Solution #3 will be discussed on the next class.
When programming with threads, processes or with any type of program that has to deal with shared data, we have to take into account all possible interleaving of these processes. In other words, in order to guarantee that concurrent processes are correct, we have to somehow guarantee that they generate the correct solution no matter how they are interleaved.
Earlier we discussed the āToo Much Milkā problem, and realized that itās very difficult to come up with an approach that always solves it properly. Let us now consider an approach that does work:
Thread A
leave note A while (note B) do nothing if (no milk) buy milk remove note A
Thread B
leave note B if (no note A) if (no milk) buy milk remove note B
This approach, unlike the two examples considered on the previous class, does work. However, it is not easy to be convinced that these two algorithms, when taken together, always produce the desired behavior. Moreover, these pieces of code have some drawbacks: first, notice that Thread A goes into a loop waiting
8-4 Lecture 8: September 25
correspond to an assembly instruction), is such that test&set(x) returns 1, if x=1; otherwise, if x=0, it returns 0 and sets x to 1. All this is of course implemented atomically. Having this type of atomic operation, one could implement thread lock(l) simply as
while test&set(l) do nothing;
and thread unlock(l) simply as
l = 0;
Summary: