Docsity

Operating Systems Lecture 8: Synchronization and Concurrency with Code Examples, Study notes of Operating Systems

Toggle sidebar
bg1
CMPSCI 377 Operating Systems Fall 2008
Lecture 8: September 25
Lecturer: Prashant Shenoy Scribe: Shashi Singh
8.1 Synchronization
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:
•Mutex: prevents things from operating on the same data at the same time;
•Critical section: a piece of code that only one thread can execute at a time;
•Lock: a mechanism for mutual exclusion; the program locks on entering a critical section, acesses the
shared data, and then unlocks. Also, a program waits if it tries to enter a locked section.
•Invariant: something that must always be true when not holding the lock.
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:
ā€¢ā€œleave a noteā€ (equivalent to a lock)
ā€¢ā€œremove a noteā€ (equivalent to a unlock)
ā€¢ā€œdon’t buy milk if there’s a noteā€ (equivalent to a wait)
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-1
pf3
pf4

Partial preview of the text

Download Operating Systems Lecture 8: Synchronization and Concurrency with Code Examples and more Study notes Operating Systems in PDF only on Docsity!

CMPSCI 377 Operating Systems Fall 2008

Lecture 8: September 25

Lecturer: Prashant Shenoy Scribe: Shashi Singh

8.1 Synchronization

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:

  • Mutex: prevents things from operating on the same data at the same time;
  • Critical section: a piece of code that only one thread can execute at a time;
  • Lock: a mechanism for mutual exclusion; the program locks on entering a critical section, acesses the shared data, and then unlocks. Also, a program waits if it tries to enter a locked section.
  • Invariant: something that must always be true when not holding the lock.

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:

  • ā€œleave a noteā€ (equivalent to a lock)
  • ā€œremove a noteā€ (equivalent to a unlock)
  • ā€œdon’t buy milk if there’s a noteā€ (equivalent to a wait)

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.

8.2 Concurrency

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:

  • Communication between threads is done implicitly, via shared variables;
  • Critical sections are regions of code that access shared variables;
  • Critical sections must be protected by synchronization;
    • We need primitives that ensure mutual exclusion;
    • Writing ā€œpersonalizedā€ solutions to concurrency is tricky and error-prone;
    • The solution is to introduce general high-level constructs into the language, such as pthread lock() and pthread unlock().