DOC PREVIEW
CMU CS 15213 - lecture

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

15 213 The course that gives CMU its Zip Synchronization November 19 2008 Topics lecture 24 ppt Synchronizing with semaphores Races and deadlocks Thread safety and reentrancy badcnt c An Improperly Synchronized Threaded Program shared volatile unsigned int cnt 0 define NITERS 100000000 int main pthread t tid1 tid2 Pthread create tid1 count Pthread create tid2 count NULL NULL NULL NULL Pthread join tid1 NULL Pthread join tid2 NULL if cnt unsigned NITERS 2 printf BOOM cnt d n cnt else printf OK cnt d n cnt 2 thread routine void count void arg int i for i 0 i NITERS i cnt return NULL linux badcnt BOOM cnt 198841183 linux badcnt BOOM cnt 198261801 linux badcnt BOOM cnt 198269672 cnt should be equal to 200 000 000 What went wrong 15 213 F 08 Assembly Code for Counter Loop C code for counter loop Corresponding asm code for i 0 i NITERS i cnt L9 movl 4 ebp eax cmpl 99999999 eax jle L12 jmp L10 Head Hi Load cnt Li Update cnt Ui Store cnt Si L12 movl cnt eax leal 1 eax edx movl edx cnt Load Update Store L11 movl 4 ebp eax leal 1 eax edx movl edx 4 ebp jmp L9 Tail Ti L10 3 15 213 F 08 Concurrent Execution Key idea In general any sequentially consistent interleaving is possible but some are incorrect 4 Ii denotes that thread i executes instruction I eaxi is the contents of eax in thread i s context i thread instri eax1 eax2 cnt 1 1 1 1 2 2 2 2 2 1 H1 L1 U1 S1 H2 L2 U2 S2 T2 T1 0 1 1 1 1 2 2 2 0 0 0 1 1 1 1 2 2 2 OK 15 213 F 08 Concurrent Execution cont Incorrect ordering two threads increment the counter but the result is 1 instead of 2 5 i thread instri eax1 eax2 cnt 1 1 1 2 2 1 1 2 2 2 H1 L1 U1 H2 L2 S1 T1 U2 S2 T2 0 1 1 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 Oops 15 213 F 08 Concurrent Execution cont How about this ordering i thread instri 1 1 2 2 2 2 1 1 1 2 H1 L1 H2 L2 U2 S2 U1 S1 T1 T2 eax1 eax2 cnt We can clarify our understanding of concurrent execution with the help of the progress graph 6 15 213 F 08 Progress Graphs A progress graph depicts the discrete execution state space of concurrent threads Thread 2 T2 L1 S2 Each axis corresponds to the sequential order of instructions in a thread S2 U2 Each point corresponds to a possible execution state Inst1 Inst2 L2 H2 H1 7 L1 U1 S1 T1 Thread 1 E g L1 S2 denotes state where thread 1 has completed L1 and thread 2 has completed S2 15 213 F 08 Trajectories in Progress Graphs Thread 2 A trajectory is a sequence of legal state transitions that describes one possible concurrent execution of the threads T2 S2 Example U2 H1 L1 U1 H2 L2 S1 T1 U2 S2 T2 L2 H2 H1 8 L1 U1 S1 T1 Thread 1 15 213 F 08 Critical Sections and Unsafe Regions Thread 2 L U and S form a critical section with respect to the shared variable cnt T2 Instructions in critical sections wrt to some shared variable should not be interleaved S2 critical section wrt cnt U2 Unsafe region Sets of states where such interleaving occurs form unsafe regions L2 H2 H1 L1 U1 S1 T1 Thread 1 critical section wrt cnt 9 15 213 F 08 Safe and Unsafe Trajectories Thread 2 T2 Safe trajectory S2 critical section wrt cnt Unsafe trajectory Unsafe region Def A trajectory is safe iff it doesn t touch any part of an unsafe region Claim A trajectory is correct wrt cnt iff it is safe U2 L2 H2 H1 L1 U1 S1 T1 Thread 1 critical section wrt cnt 10 15 213 F 08 Semaphores Question How can we guarantee a safe trajectory We must synchronize the threads so that they never enter an unsafe state Classic solution Dijkstra s P and V operations on semaphores semaphore non negative integer synchronization variable P s while s 0 wait s Dutch for Proberen test V s s Dutch for Verhogen increment OS guarantees that operations between brackets are executed indivisibly Only one P or V operation at a time can modify s When while loop in P terminates only that P can decrement s Semaphore invariant s 0 11 15 213 F 08 Locking with Semaphores Here is one way we could use P and V operations to synchronize the threads that update cnt Semaphore used like this referred to as a lock Semaphore s is initially 1 Thread routine void count void arg int i for i 0 i NITERS i P s cnt V s return NULL 12 15 213 F 08 Safe Sharing With Locks Thread 2 T2 V s 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 Forbidden region 1 S2 U2 0 0 0 0 0 1 1 1 1 1 1 1 Unsafe region 1 1 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 L2 P s H2 1 Initially s 1 13 H1 P s L1 U1 1 1 1 1 S1 V s T1 Provide mutually exclusive access to shared variable by surrounding critical section with P and V operations on semaphore s initially set to 1 Semaphore invariant creates a forbidden region that encloses unsafe region and is never touched by any trajectory Thread 1 15 213 F 08 Wrappers on POSIX Semaphores Initialize semaphore sem to value pshared 0 if thread pshared 1 if process void Sem init sem t sem int pshared unsigned int value if sem init sem pshared value 0 unix error Sem init P operation on semaphore sem void P sem t sem if sem wait sem unix error P V operation on semaphore sem void V sem t sem if sem post sem unix error V 14 15 213 F 08 Sharing With POSIX Semaphores properly sync d counter program include csapp h define NITERS 10000000 thread routine void count void arg int i volatile unsigned int cnt sem t sem semaphore for i 0 i NITERS i P sem cnt V sem return NULL int main pthread t tid1 tid2 Sem init sem 0 1 sem 1 create 2 threads and wait if cnt unsigned NITERS 2 printf BOOM cnt d n cnt else printf OK cnt d n cnt exit 0 Warning It s really slow 15 15 213 F 08 One worry races A race occurs when the correctness of the program depends on one thread reaching point x before another thread reaches point y a threaded program with a race int main pthread t tid N int i for i 0 i N i …


View Full Document

CMU CS 15213 - lecture

Documents in this Course
lecture

lecture

14 pages

lecture

lecture

46 pages

Caches

Caches

9 pages

lecture

lecture

39 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

56 pages

lecture

lecture

11 pages

lecture

lecture

9 pages

Lecture

Lecture

36 pages

Lecture

Lecture

37 pages

Exam

Exam

16 pages

Lecture

Lecture

10 pages

Lecture

Lecture

43 pages

Lecture

Lecture

8 pages

Lecture

Lecture

8 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

37 pages

Lecture

Lecture

6 pages

Lecture

Lecture

40 pages

coding

coding

2 pages

Exam

Exam

17 pages

Exam

Exam

14 pages

Lecture

Lecture

29 pages

Lecture

Lecture

34 pages

Exam

Exam

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

lecture

lecture

46 pages

Lecture

Lecture

33 pages

Lecture

Lecture

57 pages

Lecture

Lecture

32 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

11 pages

Lecture

Lecture

6 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

18 pages

Exam

Exam

10 pages

Lecture

Lecture

45 pages

Lecture

Lecture

37 pages

Exam

Exam

24 pages

class09

class09

21 pages

class22

class22

37 pages

class20

class20

30 pages

class27

class27

33 pages

class25

class25

21 pages

class04

class04

31 pages

Lecture

Lecture

59 pages

class01a

class01a

14 pages

class12

class12

45 pages

class29

class29

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

6 pages

class03

class03

34 pages

lecture

lecture

42 pages

Lecture

Lecture

40 pages

Lecture

Lecture

47 pages

Exam

Exam

19 pages

R06-B

R06-B

25 pages

class17

class17

37 pages

class25

class25

31 pages

Lecture

Lecture

15 pages

final-f06

final-f06

17 pages

Lecture

Lecture

9 pages

lecture

lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

22 pages

class11

class11

45 pages

lecture

lecture

50 pages

Linking

Linking

37 pages

Lecture

Lecture

64 pages

Integers

Integers

40 pages

Exam

Exam

11 pages

Lecture

Lecture

37 pages

Lecture

Lecture

44 pages

Lecture

Lecture

37 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

45 pages

Final

Final

25 pages

lecture

lecture

9 pages

Lecture

Lecture

30 pages

Lecture

Lecture

16 pages

Final

Final

17 pages

Lecture

Lecture

8 pages

Exam

Exam

11 pages

Lecture

Lecture

47 pages

Lecture

Lecture

9 pages

lecture

lecture

39 pages

Exam

Exam

11 pages

lecture

lecture

41 pages

Lecture

Lecture

59 pages

Lecture

Lecture

45 pages

Exam 1

Exam 1

18 pages

Lecture

Lecture

41 pages

Lecture

Lecture

32 pages

Lecture

Lecture

30 pages

Lecture

Lecture

9 pages

Lecture

Lecture

9 pages

Lecture

Lecture

15 pages

Lecture

Lecture

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

4 pages

Lecture

Lecture

46 pages

Lecture

Lecture

8 pages

Lecture

Lecture

65 pages

Lecture

Lecture

38 pages

Lecture

Lecture

35 pages

Lecture

Lecture

8 pages

Lecture

Lecture

34 pages

Lecture

Lecture

8 pages

Exam

Exam

13 pages

Lecture

Lecture

43 pages

Lecture

Lecture

9 pages

Lecture

Lecture

12 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

43 pages

Lecture

Lecture

7 pages

Lecture

Lecture

45 pages

Lecture

Lecture

24 pages

Lecture

Lecture

47 pages

Lecture

Lecture

12 pages

Lecture

Lecture

20 pages

Lecture

Lecture

9 pages

Exam

Exam

11 pages

Lecture

Lecture

52 pages

Lecture

Lecture

20 pages

Exam

Exam

11 pages

Lecture

Lecture

35 pages

Lecture

Lecture

47 pages

Lecture

Lecture

18 pages

Lecture

Lecture

30 pages

Lecture

Lecture

59 pages

Lecture

Lecture

37 pages

Lecture

Lecture

22 pages

Lecture

Lecture

35 pages

Exam

Exam

23 pages

Lecture

Lecture

9 pages

Lecture

Lecture

22 pages

class12

class12

32 pages

Lecture

Lecture

8 pages

Lecture

Lecture

39 pages

Lecture

Lecture

44 pages

Lecture

Lecture

38 pages

Lecture

Lecture

69 pages

Lecture

Lecture

41 pages

Lecture

Lecture

12 pages

Lecture

Lecture

52 pages

Lecture

Lecture

59 pages

Lecture

Lecture

39 pages

Lecture

Lecture

83 pages

Lecture

Lecture

59 pages

class01b

class01b

17 pages

Exam

Exam

21 pages

class07

class07

47 pages

Lecture

Lecture

11 pages

Odyssey

Odyssey

18 pages

multicore

multicore

66 pages

Lecture

Lecture

6 pages

lecture

lecture

41 pages

lecture

lecture

55 pages

lecture

lecture

52 pages

lecture

lecture

33 pages

lecture

lecture

46 pages

lecture

lecture

55 pages

lecture

lecture

17 pages

lecture

lecture

49 pages

Exam

Exam

17 pages

lecture

lecture

56 pages

Exam 2

Exam 2

16 pages

Exam 2

Exam 2

16 pages

Notes

Notes

37 pages

Lecture

Lecture

40 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Exam

Exam

13 pages

Lecture

Lecture

32 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

29 pages

Lecture

Lecture

59 pages

Lecture

Lecture

41 pages

Lecture

Lecture

50 pages

Exam

Exam

17 pages

Lecture

Lecture

29 pages

Lecture

Lecture

44 pages

Lecture

Lecture

41 pages

Lecture

Lecture

52 pages

Lecture

Lecture

40 pages

Lecture

Lecture

33 pages

lecture

lecture

10 pages

Lecture

Lecture

27 pages

Lecture

Lecture

29 pages

Lecture

Lecture

39 pages

Lecture

Lecture

9 pages

Lecture

Lecture

29 pages

Lecture

Lecture

8 pages

Lecture

Lecture

43 pages

Lecture

Lecture

43 pages

Lecture

Lecture

75 pages

Lecture

Lecture

55 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lecture

Lecture

35 pages

lecture

lecture

36 pages

Exam

Exam

33 pages

lecture

lecture

56 pages

lecture

lecture

64 pages

lecture

lecture

8 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

36 pages

lecture

lecture

56 pages

lecture

lecture

75 pages

lecture

lecture

36 pages

Lecture

Lecture

50 pages

Lecture

Lecture

45 pages

Lecture

Lecture

13 pages

Exam

Exam

23 pages

Lecture

Lecture

10 pages

Lecture

Lecture

48 pages

Lecture

Lecture

83 pages

lecture

lecture

57 pages

Lecture

Lecture

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

33 pages

lecture

lecture

54 pages

Lecture

Lecture

30 pages

Exam

Exam

13 pages

Lecture

Lecture

36 pages

Lecture

Lecture

40 pages

Exam

Exam

17 pages

Lecture

Lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

44 pages

Lecture

Lecture

34 pages

Lecture

Lecture

24 pages

Lecture

Lecture

29 pages

class12

class12

43 pages

lecture

lecture

43 pages

class22

class22

22 pages

R06-B

R06-B

25 pages

class01b

class01b

19 pages

lecture

lecture

29 pages

lab1

lab1

8 pages

Caches

Caches

36 pages

lecture

lecture

55 pages

Lecture,

Lecture,

37 pages

Integers

Integers

40 pages

Linking

Linking

38 pages

lecture

lecture

45 pages

Lecture

Lecture

61 pages

Linking

Linking

33 pages

lecture

lecture

40 pages

lecture

lecture

40 pages

Lecture

Lecture

32 pages

lecture

lecture

48 pages

lecture

lecture

44 pages

Exam

Exam

11 pages

Lecture

Lecture

31 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

40 pages

Exam

Exam

12 pages

Lecture

Lecture

42 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

Lecture

Lecture

13 pages

Lecture

Lecture

35 pages

Lecture

Lecture

20 pages

Final

Final

19 pages

Lecture

Lecture

33 pages

Lecture

Lecture

50 pages

Lecture

Lecture

33 pages

Lecture

Lecture

27 pages

Lecture

Lecture

6 pages

Exam

Exam

15 pages

Lecture

Lecture

24 pages

Lecture

Lecture

23 pages

Lecture

Lecture

43 pages

Lecture

Lecture

32 pages

Lecture

Lecture

52 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

15 pages

lecture

lecture

21 pages

Lecture

Lecture

58 pages

Lecture

Lecture

49 pages

Lecture

Lecture

36 pages

Lecture

Lecture

11 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

33 pages

Exam

Exam

15 pages

Lecture

Lecture

35 pages

Lecture

Lecture

10 pages

Lecture

Lecture

25 pages

Lecture

Lecture

31 pages

Lecture

Lecture

24 pages

Lecture

Lecture

34 pages

Lecture

Lecture

50 pages

lecture

lecture

35 pages

Lecture

Lecture

11 pages

Lecture

Lecture

39 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

exam1-f05

exam1-f05

11 pages

Lecture

Lecture

4 pages

Lecture

Lecture

17 pages

Exam

Exam

17 pages

malloc()

malloc()

12 pages

Lecture

Lecture

57 pages

Lecture

Lecture

30 pages

Lecture

Lecture

30 pages

Lecture

Lecture

47 pages

Lecture

Lecture

33 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lectures

Lectures

33 pages

Lecture

Lecture

36 pages

lecture

lecture

33 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Load more
Download lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lecture and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?