Concurrency Intro

18
Concurrency Intro • Interleaving (multiprogramming) produces many problems – Relative speed of two processes is not generally predictable – Must be careful when sharing global resources • O.S. Data Structures, Files, etc. – Protecting system from bad processes – Difficult to allocate resources optimally if a process is blocked or suspended • Errors tend to be difficult to find – Often dependent on exact timing • Example: Interrupt A requires 1ms to process, but error occurs if interrupt B happens during that period • Multiprocessing increases problems, but not drastically

description

Concurrency Intro. Interleaving (multiprogramming) produces many problems Relative speed of two processes is not generally predictable Must be careful when sharing global resources O.S. Data Structures, Files, etc. Protecting system from bad processes - PowerPoint PPT Presentation

Transcript of Concurrency Intro

Page 1: Concurrency Intro

Concurrency Intro• Interleaving (multiprogramming)

produces many problems– Relative speed of two processes is not

generally predictable– Must be careful when sharing global

resources• O.S. Data Structures, Files, etc.

– Protecting system from bad processes– Difficult to allocate resources optimally

if a process is blocked or suspended

• Errors tend to be difficult to find– Often dependent on exact timing

• Example: Interrupt A requires 1ms to process, but error occurs if interrupt B happens during that period

• Multiprocessing increases problems, but not drastically

Page 2: Concurrency Intro

Definitions

• Mutual Exclusion – Making sure two processes can’t both have a resource at the same time

• Critical Section – The area of a program where a resource is being used. (Processes must have mutual exclusion while executing a critical section.)

Page 3: Concurrency Intro

Definitions

• Deadlock – When two or more processes halt, unable to proceed– P1 is using A, needs B– P2 is using B, needs A

Both wait for one another indefinitely!!

(i.e. the wait is mutual)

• Starvation – A process is denied a resource indefinitely – P1 using A, P2 and P3 wait for A– P2 gets A when P1 done– P1 comes in, P1 and P3 wait for A– And so on …

Page 4: Concurrency Intro

Concurrency Requirements

• Mutual Exclusion must be enforced– Only one process at a time may be

accessing the critical section

• A process can halt outside the critical section without harm

• No deadlock or starvation (a process that wants to will eventually get into critical section)

Page 5: Concurrency Intro

Concurrency Requirements

• If no process is in a critical section, a process requesting entry must be allowed to enter without delay

• No assumptions about number or relative speed of processes or processors

• A process remains inside its critical section for a limited period of time

• Can be solved by software or hardware, with or without O.S. support

Page 6: Concurrency Intro

Use of critical section avoids race conditions!

• Process P1

-- Start CS --

Load r, n

Add r, 1

Store r, n

-- End CS --

• Process P2

-- Start CS –

Load r, n

Add r, 1

Store r, n

-- End CS --

Page 7: Concurrency Intro

Software Approaches

• Turn variable (figure 5.2a)P0 P1

while(turn!=0)/**/; while(turn!=1)/**/;

(Do nothing loop) (Do nothing loop)

critical section critical section

turn = 1; turn = 0;

– Shared variable turn indicates who is allowed to enter next

– can enter if turn = me– On exit, point variable to other process– Starvation if other process never enters

Page 8: Concurrency Intro

Software Approaches

• “Busy” Flag (figure 5.2b)P0 P1

while(flag[1])/**/; while(flag[0])

/**/;

flag[0] = true; flag[1] = true;

critical section critical section

flag[0] = false; flag[1] = false;

– Each process has a flag to indicate it is in the critical section

– Fails mutual exclusion if processes are in lockstep

*** Somebody explain this!!! ***

Page 9: Concurrency Intro

Software Approaches

• Busy Flag Modified (figure 5.2c)P0 P1

flag[0] = true; flag[1] = true;

while ( flag[1] ) /**/;while ( flag[0] )

/**/;

critical section critical section

flag[0] = false; flag[1] = false;

– Deadlocks if processes are in lockstep

*** Explain how !! ***

Page 10: Concurrency Intro

Software Approaches

• Busy Flag Again (figure 5.2d)P0 P1

flag[0] = true; flag[1] = true;

while ( flag[1] ) while ( flag[0] )

{ {

flag[0] = false; flag[1] = false;

delay delay

flag[0] = true; flag[1] = true;

} }

critical section critical section

flag[0] = false; flag[1] = false;

-- Defer if other process wants the CS– Livelock if processes are in lockstep

Page 11: Concurrency Intro

Software Approaches

• Dekker’s Algorithm (figure 5.3a)– Use flags for mutual exclusion, turn

variable to break deadlock– Handles mutual exclusion, deadlock,

and starvation

Page 12: Concurrency Intro

Software Approaches

• Peterson’s Algorithm (figure 5.3b)P0 P1

flag[0] = true; flag[1] = true;

turn = 1; turn = 0;

while ( flag[1] && while ( flag[0] &&

turn == 1 ) /**/; turn == 0 ) /**/;

critical section critical section

flag[0] = false; flag[1] = false;

• What if we have more than two processes?-- A process must check everyone else’s

flag

Page 13: Concurrency Intro

Hardware Support• Disable Interrupts

– Only works for uniprocessors– Delays response to external events

• Special Instructions– Must appear as a indivisible unit to

other processors– Test and Set – If V is 0, set to 1 and

return true, else return false• May let user specify test/set values

– Exchange – Swap reg and memory– Example uses: figure 5.5a, b

• Properties– Simple– One variable per critical section– Often use busy waiting– Possibility of deadlock or starvation

Page 14: Concurrency Intro

Semaphores

• First defined by Dijkstra

• Semaphore is an integer with a

non-negative initial value

• Two atomic operations are defined:– Wait (S)

S --;

if S<0 then block the process

else continue

– Signal (S)

S++;

if S <0, wake one blocked

process

Page 15: Concurrency Intro

Binay Semaphore

• Can only take on values 0 and 1.

• Initial value is 1

• Ideal for implementing Critical sections

S: Binare semaphore:= 1;

Process p1 Process p2

Wait (S) Wait (S)

-- CS -- -- CS --

Signal (S) Signal (S)

Page 16: Concurrency Intro

Semaphores• Provide a very useful form of mutual

exclusion– Initialize semaphore s to 1do forever:

wait(s)critical sectionsignal(s)

• Implementing Wait/Signal– Hardware primitives (Fig. 5.17, pg 229)

• Busy wait only occurs inside wait/signal

– Software methods– Semaphore internals are the “critical section”

for the low-level methods• Generally these are short enough to be acceptable

• Meaning of count:– if count 0 : how many processes can call

wait() without blocking– if count < 0 : how many processes are

currently blocked on this semaphore

Page 17: Concurrency Intro

Producers & Consumers

• One process produces some type of data• The other process consumes that data

– Data stored in a shared buffer (infinite size)

– Require mutual exclusion to access buffer

-- A Full buffer must not be

overwritten!

-- An empty buffer must not be

consumed!!

Page 18: Concurrency Intro

Producers & Consumers s: bin sem, delay: sem, n=0;• Producer

do forever

produce item

wait(s)

append to queue

n++

if n = 1 then signal(delay)

signal(s)

• Consumerwait(delay)

do forever

wait(s)

remove from queue

n--

m = n

signal(s)

if m = 0 then wait(delay)