cs399 new beginnings jonathan walpole. 2 concurrent programming & synchronization primitives

59
CS399 New Beginnings Jonathan Walpole

Upload: noel-lucas

Post on 18-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

CS399 New BeginningsJonathan Walpole

Page 2: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

2

Concurrent Programming&

Synchronization Primitives

Page 3: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

3

Concurrency

Two or more threadsParallel or pseudo parallel executionCan’t predict the relative execution speedsThe threads access shared variables

Page 4: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

4

ConcurrencyExample: One thread writes a variable that

another thread reads

Problem – non-determinism:The relative order of one thread’s reads and the other thread’s writes may determine the outcome!

Page 5: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

5

Race Conditions

What is a race condition?

Why do race conditions occur?

Page 6: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

6

Race Conditions

A simple multithreaded program with a race:

i++;

Page 7: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

7

Race Conditions

A simple multithreaded program with a race:

...load i to register;increment register; store register to i;...

Page 8: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

8

Race Conditions

Why did this race condition occur?- two or more threads have an inconsistent view of a

shared memory region (ie., a variable)- values of memory locations are replicated in

registers during execution- context switches at arbitrary times during execution- threads can see stale memory values in registers

Page 9: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

9

Race Conditions

Race condition: whenever the result depends on the precise execution order of the threads!

What solutions can we apply?- prevent context switches by preventing interrupts- make threads coordinate with each other to ensure

mutual exclusion in accessing critical sections of code

Page 10: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

10

Mutual Exclusion ConditionsNo two processes simultaneously in critical section

No assumptions about speeds or numbers of CPUs

No process running outside its critical section may block another process

No process waits forever to enter its critical section

Page 11: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

11

Mutual Exclusion Example

Page 12: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

12

Enforcing Mutual ExclusionWhat about using locks?

Locks can ensure exclusive access to shared data.- Acquiring a lock prevents concurrent access- Expresses intention to enter critical section

Assumption:- Each shared data item has an associated lock- All threads set the lock before accessing the shared data- Every thread releases the lock after it is done

Page 13: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

13

Acquiring and Releasing Locks

FreeLock

Thread A

Thread D

Thread CThread B

Page 14: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

14

Acquiring and Releasing Locks

FreeLock

Thread A

Thread D

Thread CThread B

Lock

Page 15: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

15

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock

Page 16: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

16

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock

Page 17: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

17

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Page 18: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

18

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock

Page 19: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

19

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock

Page 20: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

20

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock Lock

Lock

Page 21: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

21

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock Lock

Lock

Page 22: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

22

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock Lock

Lock

Unlock

Page 23: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

23

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock Lock

Lock

Unlock

Page 24: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

24

Acquiring and Releasing Locks

FreeLock

Thread A

Thread D

Thread CThread B

Lock Lock

Lock

Page 25: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

25

Acquiring and Releasing Locks

FreeLock

Thread A

Thread D

Thread CThread B

Lock Lock

Lock

Page 26: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

26

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock Lock

Lock

Page 27: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

27

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock Lock

Lock

Page 28: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

28

Acquiring and Releasing Locks

SetLock

Thread A

Thread D

Thread CThread B

Lock

Lock

Page 29: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

29

Mutual Exclusion (mutex) LocksAn abstract data type used for synchronization

The mutex is either:Locked (“the lock is held”)Unlocked(“the lock is free”)

Page 30: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

30

Mutex Lock OperationsLock (mutex)

Acquire the lock if it is free … and continueOtherwise wait until it can be acquired

Unlock (mutex)Release the lockIf there are waiting threads wake one up

Page 31: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

31

Using a Mutex Lock

1 repeat

2 Lock(myLock);

3 critical section

4 Unlock(myLock);

5 remainder section

6 until FALSE

1 repeat

2 Lock(myLock);

3 critical section

4 Unlock(myLock);

5 remainder section

6 until FALSE

Shared data:

Mutex myLock;

Page 32: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

32

Implementing a Mutex LockIf the lock was just a binary variable, how would we implement the lock and unlock procedures?

Page 33: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

33

Implementing a Mutex LockLock and Unlock operations must be atomic !Many computers have some limited hardware support for setting locks

- Atomic Test and Set Lock instruction- Atomic compare and swap/exchange operation- These can be used to implement mutex locks

Page 34: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

34

Test-and-Set-Lock (TSL) Instruction A lock is a single word variable with two values

0 = FALSE = not locked ; 1 = TRUE = locked Test-and-set-lock does the following atomically:

-Get the (old) value-Set the lock to TRUE-Return the old value

If the returned value was FALSE...Then you got the lock!!!

If the returned value was TRUE...Then someone else has the lock

(so try again later)

Page 35: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

35

Test and Set Lock

P1

FALSE

Lock

Page 36: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

36

Test and Set Lock

P1

Lock

FALSE

FALSE = Lock Available!!test

Page 37: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

37

Test and Set Lock

TRUE

Lock

P1

set

Page 38: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

38

Test and Set Lock

TRUE

Lock

P1 P2 P3

P4

TRUETRUE

TRUE

TRUE

TRUE

TRUE

Page 39: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

39

Test and Set Lock

TRUE

Lock

P1 P2 P3

P4

TRUETRUE

TRUE

TRUE

TRUE

TRUE

Page 40: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

40

Test and Set Lock

TRUE

Lock

P1 P2 P3

P4

TRUETRUE

TRUE

TRUE

TRUE

TRUE

Page 41: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

41

Test and Set Lock

FALSE

Lock

P1 P2 P3

P4

FALSE

TRUE

Page 42: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

42

Test and Set Lock

TRUE

Lock

P1 P2 P3

P4

TRUEFALSE

TRUE

Page 43: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

43

Test and Set Lock

TRUE

Lock

P1 P2 P3

P4

TRUEFALSE

Page 44: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

44

Test and Set Lock

TRUE

Lock

P1 P2 P3

P4

TRUE

TRUE

TRUE

TRUE

Page 45: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

45

Using TSL Directly

1 repeat2 while(TSL(lock))3 no-op;

4 critical section

5 Lock = FALSE;

6 remainder section

7 until FALSE

1 repeat2 while(TSL(lock))3 no-op;

4 critical section

5 Lock = FALSE;

6 remainder section

7 until FALSE

JI

Guarantees that only one thread at a time will enter its critical section

Page 46: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

46

Implementing a Mutex With TSL1 repeat2 while(TSL(mylock))3 no-op;

4 critical section

5 mylock = FALSE;

6 remainder section

7 until FALSE

Note that processes are busy while waiting- this kind of mutex is called a spin lock

Lock (mylock)

Unlock (mylock)

Page 47: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

47

Busy Waiting

Also called polling or spinning- The thread consumes CPU cycles to evaluate when the

lock becomes free !

Problem on a single CPU system...- A busy-waiting thread can prevent the lock holder from

running & completing its critical section & releasing the lock!

* time spent spinning is wasted on a single CPU system

- Why not block instead of busy wait ?

Page 48: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

48

Blocking PrimitivesSleep

- Put a thread to sleep- Thread becomes BLOCKED

Wakeup- Move a BLOCKED thread back onto “Ready List”- Thread becomes READY (or RUNNING)

Yield- Put calling thread on ready list and schedule next thread- Does not BLOCK the calling thread!

Just gives up the current time-slice

Page 49: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

49

How Can We Implement Them?In User Programs:

- System calls to the kernel

In Kernel:- Calls to the thread scheduler routines

Page 50: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

50

Synchronization in User ProgramsUser threads call sleep and wakeup system callsSleep and wakeup are system calls (in the kernel)

- they manipulate the “ready list”- but the ready list is shared data- the code that manipulates it is a critical section- What if a timer interrupt occurs during a sleep or wakeup

call?

Problem:- How can scheduler routines be programmed to execute

correctly in the face of concurrency?

Page 51: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

51

Concurrency in the Kernel

Solution 1: Disable interrupts during critical sectionsEnsures that interrupt handling code will not run… but what if there are multiple CPUs?

Solution 2: Use mutex locks based on TSL for critical sectionsEnsures mutual exclusion for all code that follows that

convention... but what if your hardware doesn’t have TSL?

Page 52: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

52

Disabling Interrupts

Disabling interrupts in the OS vs disabling interrupts in user processeswhy not allow user processes to disable

interrupts?is it ok to disable interrupts in the OS?what precautions should you take?

Page 53: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

53

Disabling Interrupts in the Kernel

Scenario 1: A thread is running; wants to access shared data

Disable interrupts

Access shared data (“critical section”)Enable interrupts

Page 54: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

54

Disabling Interrupts in the KernelProblem:

Interrupts are already disabled and a thread wants to access the critical section

...using the above sequence...

ie. One critical section gets nested inside another

Page 55: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

55

Disabling Interrupts in the KernelProblem: Interrupts are already disabled.

Thread wants to access critical section using the previous sequence...

Save previous interrupt status (enabled/disabled)

Disable interruptsAccess shared data (“critical section”)Restore interrupt status to what it was before

Page 56: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

56

Doesn’t Work on MultiprocessorsDisabling interrupts during critical sections

- Ensures that interrupt handling code will not run- But what if there are multiple CPUs?- A thread on a different CPU might make a system call

which invokes code that manipulates the ready queue- Disabling interrupts on one CPU didn’t prevent this!

Solution: use a mutex lock (based on TSL)- Ensures mutual exclusion for all code that uses it

Page 57: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

57

Some Other Tricky Issues

The interrupt handling code that saves interrupted state is a critical section

- It could be executed concurrently if multiple almost simultaneous interrupts happen

- Interrupts must be disabled during this (short) time period to ensure critical state is not lost

What if this interrupt handling code attempts to lock a mutex that is held?

- What happens if we sleep with interrupts disabled?- What happens if we busy wait (spin) with interrupts disabled?

Page 58: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

58

Implementing Mutex Locks Without TSL

If your CPU did not have TSL, how would you implement blocking mutex lock and unlock calls using interrupt disabling?

… this is your next Blitz project !

Page 59: CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives

59

Quiz1. What is a race condition?2. How can we protect against race conditions?3. Can locks be implemented simply by reading and

writing to a binary variable in memory?4. How can a kernel make synchronization-related

system calls atomic on a uniprocessor?- Why wouldn’t this work on a multiprocessor?

5. Why is it better to block rather than spin on a uniprocessor?

6. Why is it sometimes better to spin rather than block on a multiprocessor?