34
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

Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

Embed Size (px)

Citation preview

Page 1: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 2: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 3: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 4: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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++;

}

Page 5: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

Example: Producer/Consumer

while (1) {

while (counter == 0)

; // do nothing

nextConsumed = buffer[out];

out = (out + 1) % BUFFER_SIZE;

counter--;

// consume the item in nextConsumed

}

Page 6: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

Example: Producer/Consumer

load r1, counter

counter++ add r1, one

store r1, counter

load r2, counter

counter-- sub r2, one store r2, counter

Page 7: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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 [r1 = 5]

S1: producer executes add r1, one [r1 = 6]

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?

Page 8: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 9: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 10: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 11: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 12: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 13: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

General Structure of a Process

do {

[entry section]

critical section

[exit section]

remainder section

} while (TRUE);

Page 14: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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 grantedAssume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes

Page 15: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

Solution to Critical Section Problem

• Assume that each process executes at a

nonzero speed

• No assumption concerning relative speed of

the n processes

Page 16: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing 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

Page 17: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

Solution? #1

• Shared variables:

– int turn; initially turn = i

– turn = i Pi can enter its critical section

Process Pi Process Pj

do { do {

while (turn != i); while (turn != j);

critical section critical section

turn = j; turn = i;

remainder section remainder section

} while (TRUE); } while (TRUE);

• How good is this solution in terms of solution to CS problem requirements? (mutual exclusive yes,

progress, no)

Page 18: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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 Process Pj

do { do {

flag[i] = true; flag[j] = true;

while (flag[j]); while (flag[i]);

critical section critical section

flag[i] = false; flag[j] = false;

remainder section remainder section

} while (TRUE); } while (TRUE);

• How good is this solution in terms of solution to CS problem requirements? (no progress)

Page 19: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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!

Page 20: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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 critical section

flag[i] = false; flag[j] = false;

remainder section remainder section

} while (TRUE); } while (TRUE);

• How good is this solution in terms of solution to CS problem requirements?

Page 21: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

21

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

Page 22: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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...

Page 23: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 24: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

Bakery Algorithm

while(1)}sectionremainder ;0][

section critical}

)));],[()],[&((&)0]![((while]);[(while

{ );;0(for;][

;1])1[],1[],0[max(][;][

{ do Process

inumber

iinumberjjnumberjnumberjchoosing

jnjjfalseichoosing

nnumbernumbernumberinumbertrueichoosing

Pi

Creating a number (first part of ticket)

Awaiting for permission to enter CS

Page 25: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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:

Person BPerson ATime

Arrive home, put milk away3:30

Buy milk3:25

Arrive at storeArrive home, put milk away3:20

Leave for storeBuy milk3:15

Leave for store3:05

Look in Fridge. Out of milk3:00

Look in Fridge. Out of milkArrive at store3:10

Page 26: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

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

Page 27: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

“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

Page 28: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

“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):

if (noMilk) { if (noNote) { leave Note; buy milk; remove note; }

}• Result?

– 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…

Page 29: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

“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

Page 30: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

“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

Page 31: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

“Too Much Milk” Solution? #3• Here is a possible two-note solution:

Process A Process Bleave note A; leave note B;while (note B) { //X if (noNote A) { //Y do nothing; if (noMilk) {} buy milk;if (noMilk) { } buy milk; }} remove note B;remove note A;

• 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

Page 32: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

“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

Page 33: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

“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();

Page 34: Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes

Where are we going with synchronization?

• 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

Load/Store Disable Ints Test&Set Comp&Swap

Locks Semaphores Monitors

Shared Programs

Hardware

Higher-level

API

Programs