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:

Carnegie Mellon Introduction to Computer Systems 15 213 18 243 spring 2009 27th Lecture Apr 30th Instructors Gregory Kesden and Markus P schel Carnegie Mellon Today Races deadlocks thread safety Multi code Thread Level Parallelism TLP Simultaneous Multi Threading SMT Carnegie Mellon Another worry Deadlock Processes wait for condition that will never be true Typical Scenario Processes 1 and 2 needs two resources A and B to proceed Process 1 acquires A waits for B Process 2 acquires B waits for A Both will wait forever Carnegie Mellon Deadlocking With POSIX Semaphores int main pthread t tid 2 Sem init mutex 0 0 1 mutex 0 1 Sem init mutex 1 0 1 mutex 1 1 Pthread create tid 0 NULL count void 0 Pthread create tid 1 NULL count void 1 Pthread join tid 0 NULL Pthread join tid 1 NULL printf cnt d n cnt exit 0 void count void vargp int i int id int vargp for i 0 i NITERS i P mutex id P mutex 1 id cnt V mutex id V mutex 1 id return NULL Tid 0 P s0 P s1 cnt V s0 V s1 Tid 1 P s1 P s0 cnt V s1 V s0 Carnegie Mellon Deadlock Visualized in Progress Graph Thread 2 V s0 deadlock state Forbidden region for s1 Locking introduces the potential for deadlock waiting for a condition that will never be true V s1 Any trajectory that enters the deadlock region will eventually reach the deadlock state waiting for P s0 either s0 or s1 to become nonzero P s1 deadlock region P s0 s0 s1 1 Forbidden region for s2 P s1 V s0 V s1 Other trajectories luck out and skirt the deadlock region Thread 1 Unfortunate fact deadlock is often non deterministic Carnegie Mellon Avoiding Deadlock Acquire shared resources in same order int main pthread t tid 2 Sem init mutex 0 0 1 mutex 0 1 Sem init mutex 1 0 1 mutex 1 1 Pthread create tid 0 NULL count void 0 Pthread create tid 1 NULL count void 1 Pthread join tid 0 NULL Pthread join tid 1 NULL printf cnt d n cnt exit 0 void count void vargp int i int id int vargp for i 0 i NITERS i P mutex 0 P mutex 1 cnt V mutex id V mutex 1 id return NULL Tid 0 P s0 P s1 cnt V s0 V s1 Tid 1 P s0 P s1 cnt V s1 V s0 Carnegie Mellon Avoided Deadlock in Progress Graph Thread 2 V s1 No way for trajectory to get stuck Processes acquire locks in same order Forbidden region for s1 Order in which locks released immaterial V s0 P s1 Forbidden region for s2 P s0 Thread 1 P s0 s0 s1 1 P s1 V s0 V s1 Carnegie Mellon Crucial concept Thread Safety Functions called from a thread without external synchronization must be thread safe Meaning it must always produce correct results when called repeatedly from multiple concurrent threads Some examples of thread unsafe functions Failing to protect shared variables Relying on persistent state across invocations Returning a pointer to a static variable Calling thread unsafe functions Carnegie Mellon Thread Unsafe Functions Class 1 Failing to protect shared variables Fix Use P and V semaphore operations Example goodcnt c Issue Synchronization operations will slow down code e g badcnt requires 0 5s goodcnt requires 7 9s Carnegie Mellon Thread Unsafe Functions Class 2 Relying on persistent state across multiple function invocations Example Random number generator RNG that relies on static state rand return pseudo random integer on 0 32767 static unsigned int next 1 int rand void next next 1103515245 12345 return unsigned int next 65536 32768 srand set seed for rand void srand unsigned int seed next seed Carnegie Mellon Making Thread Safe RNG Pass state as part of argument and thereby eliminate static state rand return pseudo random integer on 0 32767 int rand r int nextp nextp nextp 1103515245 12345 return unsigned int nextp 65536 32768 Consequence programmer using rand must maintain seed Carnegie Mellon Thread Unsafe Functions Class 3 Returning a ptr to a static variable Fixes 1 Rewrite code so caller passes pointer to struct Issue Requires changes in caller and callee 2 Lock and copy Issue Requires only simple changes in caller and none in callee However caller must free memory struct hostent gethostbyname char name static struct hostent h contact DNS and fill in h return h hostp Malloc gethostbyname r name hostp struct hostent gethostbyname ts char name struct hostent q Malloc struct hostent p P mutex lock p gethostbyname name q p copy V mutex return q Carnegie Mellon Thread Unsafe Functions Class 4 Calling thread unsafe functions Calling one thread unsafe function makes the entire function that calls it thread unsafe Fix Modify the function so it calls only thread safe functions Carnegie Mellon Thread Safe Library Functions All functions in the Standard C Library at the back of your K R text are thread safe Examples malloc free printf scanf Most Unix system calls are thread safe with a few exceptions Thread unsafe function Class asctime ctime gethostbyaddr gethostbyname inet ntoa localtime rand Reentrant version 3 asctime r 3 ctime r 3 gethostbyaddr r 3 gethostbyname r 3 none 3 localtime r 2 rand r Carnegie Mellon Notifying With Semaphores producer thread shared buffer consumer thread Common synchronization pattern Producer waits for slot inserts item in buffer and notifies consumer Consumer waits for item removes it from buffer and notifies producer Examples Multimedia processing Producer creates MPEG video frames consumer renders them Event driven graphical user interfaces Producer detects mouse clicks mouse movements and keyboard hits and inserts corresponding events in buffer Consumer retrieves events from buffer and paints the display Carnegie Mellon Producer Consumer on a Buffer That Holds One Item buf1 c producer consumer on 1 element buffer include csapp h int main pthread t tid producer pthread t tid consumer define NITERS 5 initialize the semaphores Sem init shared empty 0 1 Sem init shared full 0 0 void producer void arg void consumer void arg create threads and wait Pthread create tid producer NULL producer NULL Pthread create tid consumer NULL consumer NULL Pthread join tid producer NULL Pthread join tid consumer NULL struct int buf shared var sem t full sems sem t empty shared exit 0 Carnegie Mellon Producer Consumer cont Initially empty 1 full 0 producer thread void producer void arg int i item consumer thread void consumer void arg int i item for i 0 i NITERS i read item from buf P shared full item shared buf V shared empty for i 0 i NITERS i produce item item i printf produced d n item write item to buf P shared empty shared buf item V shared full return NULL consume item printf consumed d n item return NULL Carnegie Mellon Counting with Semaphores


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

37 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

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?