DOC PREVIEW
CMU CS 15213 - Recitation

This preview shows page 1-2-14-15-30-31 out of 31 pages.

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

Unformatted text preview:

Thread 15213 S04 Recitation Section A Thread Memory Model Thread Interfaces System Calls Thread Safety Pitfalls of Using Thread Racing Semaphore Final Evaluation Forms pthread create pthread join pthread self pthread cancel pthread exit pthread mutex init pthread mutex un lock pthread cond init pthread cond timed wait 2 15 213 S 04 Why Do We Care About Thread Useful for L7 Part II A very important way to implement modern concurrent systems What s concurrency Web Server Web Browser Web Browser Web Browser 3 Proxy Web Server Web Server 15 213 S 04 Three Methods to Implement Concurrency 1 Processes Fork a child process for every incoming client connection Difficult to share data among child processes 2 Threads Create a thread to handle every incoming client connection Our focus today 3 I O multiplexing with Unix select Use select to notice pending socket activity Manually interleave the processing of multiple open connections More complex implementing your own app specific thread package 4 15 213 S 04 View of Process Process process context code data and stack Process context Program context Code data and stack stack SP Data registers Condition code Stack pointer SP Program counter PC Kernel context VM structures Descriptor table 5 shared libraries run time heap read write data PC read only code data 0 15 213 S 04 View of Thread Thread memory model Multiple threads can be associated with a process Each thread has its own logical control flow instruction flow Each thread has its own thread ID TID Each thread shares the same code data and kernel context Thread 1 main thread Shared code and data shared libraries run time heap read write data read only code data stack 1 Thread 1 context Data registers Condition code SP1 PC1 6 0 Kernel context VM structures Descriptor table Thread 2 peer thread stack 2 Thread 2 context Data registers Condition code SP2 PC2 15 213 S 04 Posix Threads Pthreads Interface Standard interface for 60 functions Creating and reaping threads pthread create pthread join Determining your thread ID pthread self Terminating threads pthread cancel pthread exit Synchronizing access to shared variables pthread mutex init pthread mutex un lock pthread cond init pthread cond timed wait 7 15 213 S 04 The Pthread hello world Program hello c Pthreads hello world program include csapp h thread routine void thread void vargp printf Hello world n return NULL int main pthread t tid Pthread create tid NULL thread NULL Pthread join tid NULL exit 0 8 15 213 S 04 Execution of Threaded hello world main thread Call Pthread create Pthread create returns peer thread call Pthread join main thread waits for peer thread to terminate printf return NULL peer thread terminates Pthread join returns exit terminates main thread and any peer threads 9 15 213 S 04 Practices Basic usage of thread Pthread exit exit Joinable and detached thread Thread safety 10 Protecting shared variable Function that returns a static pointer more 15 213 S 04 pthread exit exit Program 1 1 void thread void vargp pthread exit void 42 int main int i pthread t tid pthread create tid NULL thread NULL pthread join tid void i printf d n i 11 15 213 S 04 pthread exit exit Program 1 2 void thread void vargp exit 42 int main int i pthread t tid pthread create tid NULL thread NULL pthread join tid void i printf d n i 12 15 213 S 04 pthread exit exit pthread exit only terminates the current thread NOT the process Exit terminates all the threads in the process i e the process itself 13 15 213 S 04 Practices Basic usage of thread Pthread exit exit Joinable and detached thread Thread safety 14 Protecting shared variable Function that returns a static pointer more 15 213 S 04 Joinable Detached Threads At any point in time a thread is either joinable or detached Joinable thread can be reaped and killed by other threads must be reaped with pthread join to free memory resources Detached thread cannot be reaped or killed by other threads resources are automatically reaped on termination Default state is joinable 15 use pthread detach pthread self to make detached 15 213 S 04 Practices Basic usage of thread Pthread exit exit Joinable and detached thread Thread safety 16 Protecting shared variable Function that returns a static pointer more 15 213 S 04 Protecting shared variables Program 1 5 int i 42 void thread void vargp printf d n i void thread2 void vargp i 31 int main pthread t tid tid2 pthread create tid2 NULL thread2 void i pthread create tid NULL thread void i pthread join tid void i pthread join tid2 NULL 17 15 213 S 04 Practices Basic usage of thread Pthread exit exit Joinable and detached thread Thread safety 18 Protecting shared variable Function that returns a static pointer more 15 213 S 04 Functions that return a pointer to a static value int main struct in addr a a s addr inet addr 1 1 1 1 printf s n inet ntoa a Output 1 1 1 1 int main struct in addr a b a s addr inet addr 1 1 1 1 b s addr inet addr 2 2 2 2 printf s s n inet ntoa a inet ntoa b output 19 15 213 S 04 Thread Safety Class 1 Functions that do not protect shared variables Class 2 Functions that keep state across multiple invocations rand Textbook P 886 Class 3 Functions that return a pointer to a static variable Class 4 Functions that call thread unsafe functions 20 May or may not be thread unsafe Textbook P 887 15 213 S 04 More Practice Problems Program 1 3 1 4 are left for your practice 21 15 213 S 04 Racing A race occurs when the correctness of a program depends on one thread reaching point x in its control flow before another thread reaches point y Generally related with the access to the shared variables Need synchronization Ways to do synchronization 22 Semaphores Mutual exclusion 15 213 S 04 Program 2 b void foo void vargp int id id int vargp printf Thread d n id int main pthread t tid 2 int i Racing for i 0 i 2 i Pthread create tid i NULL foo i Pthread join tid 0 NULL Pthread join tid 1 NULL 23 15 213 S 04 Program 2 a void foo void vargp int myid myid int vargp Free vargp printf Thread d n myid int main pthread t tid 2 int i ptr Good for i 0 i 2 i ptr Malloc sizeof int ptr i Pthread create tid i 0 foo ptr Pthread join tid 0 0 Pthread join tid 1 0 24 15 213 S 04 Program 2 c void foo void vargp int id id int vargp printf Thread d n id int main pthread t tid 2 int i Good for i 0 i 2 i Pthread create tid i 0 foo i Pthread join tid 0 0 Pthread join tid 1 0 25 15 213 S 04 The general solution for …


View Full Document

CMU CS 15213 - Recitation

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

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 Recitation
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 Recitation 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 Recitation 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?