OS Spring’04 Concurrency Operating Systems Spring 2004.

40
OS Spring’04 Concurrency Operating Systems Spring 2004
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    228
  • download

    4

Transcript of OS Spring’04 Concurrency Operating Systems Spring 2004.

Page 1: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Concurrency

Operating Systems Spring 2004

Page 2: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Concurrency pros and cons Concurrency is good for users

One of the reasons for multiprogramming Working on the same problem, simultaneous

execution of programs, background execution

Concurrency is a “pain in the neck” for the system

Access to shared data structuresDeadlock due to resource contention

Page 3: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Mutual Exclusion OS is an instance of concurrent

programmingMultiple activities may take place in the same time

Concurrent execution of operations involving multiple steps is problematic

Example: updating linked list Concurrent access to a shared data

structure must be mutually exclusive

Page 4: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

new->next=current.next

current

new

current

new

current.next=new

insert_after(current,new):

Page 5: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

tmp=current.next;current.next=current.next.next;free(tmp);

current

current

remove_next(current):

Page 6: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

current

new

current

new

current

new

Page 7: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Atomic operations A generic solution is to ensure

atomic execution of operationsAll the steps are perceived as executed in a single point of time

insert_after(current,new) remove_next(current), or

remove_next(current) insert_after(current,new)

Page 8: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

The Critical Section Model A code within a

critical section must be executed exclusively by a single process

do {

critical section

remainder section

} while(1)

entry section

exit section

Page 9: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Linked list exampledo {

remainder section

} while(1)

entry section

exit section

do {

remainder section

} while(1)

entry section

exit section

new->next=current.next

current.next=new

tmp=current.next;current.next=current.next.next;free(tmp);

Page 10: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

The Critical Section Problem n processes P0,…,Pn-1

No assumptions on relative process speeds, no synchronized clocks, etc…

Models inherent non-determinism of process scheduling

No assumptions on process activity when executing within

Critical and reminder sections

Page 11: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Shared variables Processes are communicating

through shared atomic read/write variables

x is a shared variable, l is a local variableRead: takes the current value:

Write: assigns a provided value:

){...}6(

;

xif

xl

;5x

Page 12: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Requirements Mutual Exclusion: If process Pi is

executing its C.S., then no other process is in its C.S.

Progress: If Pi is in its entry section and no process is in C.S., then some process eventually enters C.S.

Fairness: If no process remains in C.S. forever, then each process requesting entry to C.S. will be eventually let into C.S.

Page 13: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Solving the CS problem (n=2)

while(1)}

sectionremainder

section critical

);!( while

;

{ do

: Process

nothingdo

iturn

iturn

Pi

;:Shared turn {0,1} enum

Page 14: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Solving the CS problem (n=2)

while(1)}

sectionremainder

;1

section critical

);( while

{ do

; Process

iturn

iturn !

Pi

;:Shared turn {0,1} enum

Page 15: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Solving the CS problem (n=2)

while(1)}

sectionremainder

;][

section critical

]);1[(

;][

{ do

: Process

falseiflag

iflagwhile

trueiflag

Pi

]2[ :Shared flagboolean

Page 16: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Peterson’s algorithm for n=2

while(1)}

sectionremainder

;][

section critical

);1&&]1[(

;1

;][

{ do

: Process

falseiflag

iturniflagwhile

iturn

trueiflag

Pi

; {0,1}];2[ :Shared turn enumflagboolean

Page 17: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Bakery algorithm of Lamport Critical section algorithm for any

n>1 Each time a process is requesting an

entry to CS, assign it a ticket which is

Unique and monotonically increasing

Let the process into CS in the order of their numbers

Page 18: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Choosing a ticket;[n]boolean];n[ int:Shared choosing number

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

)],[()],[( iinumberjjnumber Does not guarantee uniqueness! Use process Ids:

;][

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

;][

falseichoosing

nnumbernumbernumberinumber

trueichoosing

Process need to know that somebody perhaps chose a smaller number:

Page 19: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Bakery algorithm for n processes

while(1)}sectionremainder ;0][

section critical}

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

{ );;0(for;][

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

{ do Process

inumber

iinumberjjnumberjnumberjchoosing

jnjjfalseichoosing

nnumbernumbernumberinumbertrueichoosing

Pi

Page 20: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Correctness

)],[()],[(0][

thensection, critical in the is if ,any For

iinumberjjnumberjnumber

Pij j

Lemma:

Mutual exclusion is immediate from this lemma

It is easy to show that Progress and Fairness holdas well (recitation)

Page 21: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Hardware primitives Elementary building blocks capable

of performing certain steps atomically

Should be universal to allow for solving versatile synchronization problems

Numerous such primitives were identified:

Test-and-setFetch-and-addCompare-and-swap

Page 22: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Test-and-Set (TS)boolean test-and-set(boolean &lock){

temp=lock;lock=TRUE;return temp;

}

reset(boolean &lock){

lock=FALSE;}

Page 23: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Critical section using TSShared boolean lock, initially

FALSE

do {while(test-and-set(&lock));

critical section;reset(&lock);

reminder section;} while(1);

Check yourself!•Is mutual exclusion satisfied?•Is progress satisfied?•Is fairness satisfied?

Page 24: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Discussion Satisfies Mutual Exclusion and

Progress Does not satisfy Fairness Provides exclusion among

unbounded number of processesProcess IDs and number are unknown

Busy waitingBurning CPU cycles while being blocked

Page 25: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Fetch-and-Add (FAA)

s: shared, a: local

int FAA(int &s, int a){

temp=s;s=s+a;return temp;

}FAA can be used as a ticket machine

Page 26: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Critical section using FAAShared: int s, turn; Initially: s = 0; turn=0;

Process Pi code:Entry:

me = FAA(s,1);while(turn < me); // busy wait for my turn

Critical sectionExit:

FAA(turn,1);

Check yourself!•Is mutual exclusion satisfied?•Is progress satisfied?•Is fairness satisfied?

Page 27: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Discussion Satisfies all three properties Supports unbounded number of

processes Unbounded counter Busy waiting

Page 28: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Problems with studied synchronization methods Critical section framework is

inconvenient for programming Performance penalty

Busy waitingToo coarse synchronization

Using hardware primitives directly results in non-portable code

Page 29: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Higher Level Abstractions Higher level software abstractions

are represented bySemaphoresMonitors

Page 30: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Semaphores Invented by Edsger Dijkstra in 1968 Interface consists of two primitives:

P() and V()

}

;

);0(

{)(

S

Swhile

SP

}

;

{)(

S

SV

Page 31: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Notes on the Language Dutch: P: Proberen, V: Verhogen Hebrew: P: חותפ , V: עודו English: P(): wait(), V(): signal()

Page 32: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Semaphores: initial value Initial value of a semaphore

indicates how many identical instances of the critical resource exist

A semaphore initialized to 1 is called a mutex (mutual exclusion)

Page 33: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Programming with semaphores Semaphores is a powerful

programming abstraction Define a semaphore for each critical

resourceE.g., one for each linked listGranularity?

Concurrent processes access appropriate semaphores when synchronization is needed

Page 34: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Some examples…………V(synch);……

…P(synch);……………

Page 35: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Some examplesDo {

P(mutex);

critical section

V(mutex);

Remainder section;

While(1);

Page 36: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Implementing semaphores Semaphores can be implemented

efficiently by the systemP() is explicitly telling the system: “Hey, I cannot proceed, you can preempt me”V() instructs the system to wake up a waiting process

Page 37: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Implementing Semaphores

type semaphore = record count: integer; queue: list of process end;var S: semaphore;

S.count must be initialized to a nonnegative value (depending on application)

Page 38: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Implementing Semaphores

P(S): S.count--; if (S.count<0) { add this process to S.queue block this process; }

V(S): S.count++; if (S.count <= 0) { remove a process P from S.queue place this process P on ready queue }

Page 39: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

We’re still cheating… P() and V() must be executed

atomically In uniprocessor system may disable

interrupts In multi-processor system, use

hardware synchronization primitivesTS, FAA, etc…

Involves a some limited amount of busy waiting

Page 40: OS Spring’04 Concurrency Operating Systems Spring 2004.

OS Spring’04

Monitorsmonitor monitor-name{

shared variable declarations

procedure P1(…) {…

}…

procedure Pn() {…

}}

Only a single process at a time can be active within the monitor

=> other processes calling Pi() are queued

Conditional variables for finer grain synchronization

x.wait() suspend the execution until another process calls x.signal()