Process Synchronization 1

advertisement
Process Synchronization
• A set of concurrent/parallel processes/tasks can be disjoint or cooperating
(or competing)
•
With cooperating and competing processes we are going to have situations that are
irreproducible and unpredictable
Example: ATM Bank Server
•
ATM server problem:
–
Service a set of requests
–
Do so without corrupting database
–
Maintain correct balance
–
Don’t hand out too much money
Example: ATM Bank Server
Deposit(acctId, amount) {
acct = GetAccount(actId);
acct->balance += amount;
StoreAccount(acct);
}
•
Unfortunately, shared state can get corrupted:
Process1
Process 2
load r1, acct->balance
load r2, acct->balance
add r2, amount2
store r2, acct->balance
add r1, amount1
store r1, acct->balance
Example: Producer/Consumer
while (1) {
while (counter == BUFFER_SIZE)
; // do nothing
// produce an item and put it in nextProduced
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Example: Producer/Consumer
while (1) {
while (counter == 0)
; // do nothing
nextConsumed =
buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
// consume the item in nextConsumed
}
Example: Producer/Consumer
counter++
load r1, counter
add r1, one
store r1, counter
counter--
load r2, counter
sub r2, one
store r2, counter
Example: Producer/Consumer
•
•
load r1, counter
load r2, counter
add r1, one
sub r2, one
store r1, counter
store r2, counter
Consider this execution interleaving:
S0:
producer executes load r1, counter
S1:
producer executes add r1, one
S2:
consumer executes load r2, counter
[r2 = 5]
S3:
consumer executes sub r2, one
[r2 = 4]
S4:
producer executes store r1, counter
[counter = 6]
S5:
consumer executes store r2, counter
[counter = 4]
What execution interleaving gives 5 or 6?
[r1 = 5]
[r1 = 6]
Race Condition
• A situation where several processes access and manipulate
the same data concurrently and the outcome of the execution
depends on the particular order in which the access takes
place
Atomic Operations
• Atomic Operation: an operation that always runs to completion or not at
all
– It is indivisible: it cannot be stopped in the middle and state cannot be
modified by someone else in the middle
– Fundamental building block – if no atomic operations, then have no way for
processes to work together
• On most machines, memory references and assignments (i.e., loads and
stores) of words are atomic. Many instructions are not atomic
Atomic Operations
• Bottom level indivisible operation is architecture dependent.
Typically, it is whatever takes place in one CPU cycle.
Everything else can be divided
• Lowest level atomic operation is called memory interlock or
hardware arbiter. Everything else is built on top of that
Critical Section
•
In order to avoid having these unpredictable situations we need some way of
synchronizing (establishing order) processes at their point of interaction
•
The critical section is the segment of code in which the process may be changing
common variables, updating a table, writing a file, and so on (i.e., segment of code
containing at least one shared variable)
•
When one process is executing in its critical section, no other process should be
allowed to execute in its critical section. That is, no two processes should be
allowed to execute in their critical sections at the same time
Critical Section
• Critical sections are used to artificially create indivisible
operations
• The critical section problem is to design a protocol that
processes can use to cooperate. Each process must request
permission to enter its critical section
General Structure of a Process
do {
[entry section]
critical section
[exit section]
remainder section
} while (TRUE);
Solution to Critical-Section
Problem
1. Mutual Exclusion - If process Pi is executing in its critical section,
then no other processes can be executing in their critical sections
2. Progress - If no process is executing in its critical section and there
exist some processes that wish to enter their critical section, then
the selection of the processes that will enter the critical section
next cannot be postponed indefinitely (i.e., it is not turn-taking)
3. Bounded Waiting - A bound must exist on the number of times that
other processes are allowed to enter their critical sections after a
process has made a request to enter its critical section and before
that request is granted
 Assume that each process executes at a nonzero speed
 No assumption concerning relative speed of the N processes
Solution to Critical Section Problem
• Assume that each process executes at a
nonzero speed
• No assumption concerning relative speed of
the n processes
Initial Attempts to Solve Problem
•
Only 2 processes, Pi and Pj
•
General structure of a process:
do {
[entry section]
critical section
[exit section]
remainder section
}
•
while (TRUE);
Processes may share some common variables to synchronize their actions
Solution? #1
•
Shared variables:
–
int turn; initially turn = i
–
turn = i  Pi can enter its critical section
Process Pi
do {
do {
while (turn != i);
critical section
turn = j;
remainder section
} while (TRUE);
•
Process Pj
while (turn != j);
critical section
turn = i;
remainder section
} while (TRUE);
How good is this solution in terms of solution to CS problem requirements? (mutual exclusive yes,
Solution? #2
•
Shared variables:
–
boolean flag[2]; initially flag[0] = flag[1] = false
–
flag[i] = true  Pi ready to enter its critical section
Process Pi
do {
do {
flag[i] = true;
flag[j] = true;
while (flag[j]);
while (flag[i]);
critical section
flag[i] = false;
remainder section
} while (TRUE);
•
Process Pj
critical section
flag[j] = false;
remainder section
} while (TRUE);
How good is this solution in terms of solution to CS problem requirements? (no progress)
Peterson’s Solution
• Two process solution
• The two processes share two variables:
– int turn;
– Boolean flag[2]
• The variable turn indicates whose turn it is to enter the critical
section.
• The flag array is used to indicate if a process is ready to
enter the critical section. flag[i] = true implies that process Pi
is ready!
Solution? #3
•
Combined shared variables of solutions #1 and #2
Process Pi
Process Pj
do {
do {
flag[i] = true;
flag[j] = true;
turn = j;
turn = i;
while (flag[j] and turn == j);
while (flag[i] and turn == i);
critical section
flag[i] = false;
remainder section
} while (TRUE);
•
critical section
flag[j] = false;
remainder section
} while (TRUE);
How good is this solution in terms of solution to CS problem requirements?
Satisfying the properties
• Mutual exclusion
– turn must be 0 or 1 => only one thread can be in CS
• Progress
– only one thread trying to get into CS => flag[other] is false
=> will get in
• Bounded Waiting
– spinning thread will not modify turn
– thread trying to go back in will set turn equal to spinning
thread
21
Bakery Algorithm
• Critical section problem for n processes
• Before entering its critical section, process receives a number. Holder of
the smallest number enters the critical section
• If processes Pi and Pj receive the same number, if i < j, then Pi is served
first; else Pj is served first
• The numbering scheme always generates numbers in increasing order of
enumeration; i.e., 1,2,3,3,3,3,4,5...
Bakery Algorithm
• Notation <  lexicographical order (ticket #, process id #)
(a,b) < (c,d) if a < c or if a = c and b < d
• Shared data
boolean choosing[n];
int number[n];
Data structures are initialized to false and 0 respectively
Bakery Algorithm
Creating a number (first part
of ticket)
P rocessPi
do {
choosing[i ]  true;
num ber[i ]  max(num ber[0], num ber[1], num ber[n  1])  1;
choosing[i ]  false;
for( j  0; j  n; j  ) {
while(choosing[ j ]);
while((num ber[ j ]! 0) & &((num ber[ j ], j )  (num ber[i ], i )));
}
criticalsection
Awaiting for permission to
num ber[i ]  0;
enter CS
remaindersection
} while(1)
Motivation: “Too much milk”
•
•
Great thing about OS’s – analogy between problems in OS and problems in real life
–
Help you understand real life problems better
–
But, computers are much stupider than people
Example: People need to coordinate:
Time
3:00
3:05
3:10
3:15
3:20
3:25
3:30
Person A
Look in Fridge. Out of milk
Leave for store
Arrive at store
Buy milk
Arrive home, put milk away
Person B
Look in Fridge. Out of milk
Leave for store
Arrive at store
Buy milk
Arrive home, put milk away
Lock
•
Lock: prevents someone from doing something
– Lock before entering critical section and before accessing shared data
– Unlock when leaving, after accessing shared data
– Wait if locked (Important idea: all synchronization involves waiting)
•
For example: fix the milk problem by putting a key on the refrigerator
– Lock it and take key if you are going to go buy milk
– Fixes too much: roommate angry if only wants something else
– Of Course – We don’t know how to make a lock yet
“Too Much Milk” Problem
• What are the correctness properties for the “Too much milk”
problem???
– Never more than one person buys
– Someone buys if needed
• Restrict ourselves to use only atomic load and store
operations as building blocks
“Too Much Milk” Solution? #1
• Use a note to avoid buying too much milk:
– Leave a note before buying (kind of “lock”)
– Remove note after buying (kind of “unlock”)
– Don’t buy if note (wait)
• Suppose a computer tries this (remember, only memory read/write are
atomic):
• Result?
if (noMilk) {
if (noNote) {
leave Note;
buy milk;
remove note;
}
}
– Still too much milk but only occasionally!
– Process can get context switched after checking milk and note but before
leaving note!
• Solution makes problem worse since fails intermittently
– Makes it really hard to debug…
“Too Much Milk” Solution? #1½
• Clearly the Note is not quite blocking enough
– Let’s try to fix this by placing note first
• Another try at previous solution:
leave Note;
if (noMilk) {
if (noNote) {
buy milk;
}
}
remove note;
• What happens here?
– Well, with human, probably nothing bad
– With computer: no one ever buys milk
“Too Much Milk” Solution? #2
•
How about labeled notes?
–
Now we can leave note before checking
Process A
Process B
leave note A;
leave note B;
if (noNote B) {
if (noNote A) {
if (noMilk) {
if (noMilk) {
buy Milk;
buy Milk;
}
}
}
remove note A;
remove note B;
•
Does this work?
•
Possible for neither process to buy milk
–
•
}
Context switches at exactly the wrong times can lead each to think that the other is going to buy
Extremely unlikely that this would happen, but will at worse possible time
“Too Much Milk” Solution? #3
• Here is a possible two-note solution:
Process A
leave note A;
while (note B) { //X
do nothing;
}
if (noMilk) {
buy milk;
}
remove note A;
Process B
leave note B;
if (noNote A) { //Y
if (noMilk) {
buy milk;
}
}
remove note B;
• Does this work? Yes. Both can guarantee that:
– It is safe to buy, or
– Other will buy, ok to quit
• At X:
– if no note B, safe for A to buy,
– otherwise wait to find out what will happen
• At Y:
– if no note A, safe for B to buy
– Otherwise, A is either buying or waiting for B to quit
“Too Much Milk” Solution #3 Discussion
•
Our solution protects a single CS piece of code for each process:
if (noMilk) {
buy milk;
}
•
Solution #3 works, but it’s really unsatisfactory
– Really complex – even for this simple an example
» Hard to convince yourself that this really works
– A’s code is different from B’s – what if lots of processes?
» Code would have to be slightly different for each process
– While A is waiting, it is consuming CPU time
» This is called “busy-waiting”
•
There’s a better way
– Have hardware provide better (higher-level) primitives than atomic load and
store
– Build even higher-level programming abstractions on this new hardware
support
“Too Much Milk” Solution #4
• Suppose we have some sort of implementation of a lock
– Lock.Acquire() – wait until lock is free, then grab
– Lock.Release() – Unlock, waking up anyone waiting
– These must be atomic operations – if two processes are waiting for
the lock and both see it’s free, only one succeeds to grab the lock
• Then, our milk problem is easy:
milklock.Acquire();
if (nomilk)
buy milk;
milklock.Release();
Where are we going with synchronization?
Programs
Shared Programs
Higher-level
API
Locks Semaphores Monitors
Hardware
Load/Store Disable Ints Test&Set Comp&Swap
• We are going to implement various higher-level synchronization primitives
using atomic operations
– Everything is pretty painful if only atomic primitives are load and store
– Need to provide primitives useful at user-level
Download