DOC PREVIEW
CMU CS 15213 - ConcurrencyI:Threads

This preview shows page 1-2 out of 6 pages.

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

Unformatted text preview:

Traditional view of a process 15 213 The course that gives CMU its Zip Process process context code data and stack Concurrency I Threads April 10 2001 Process context Data registers Condition codes Stack pointer SP Program counter PC Topics Code data and stack Program context stack SP shared libraries brk run time heap Kernel context Thread concept Posix threads Pthreads interface Linux Pthreads implementation Concurrent execution Sharing data read write data VM structures Open files Signal handlers brk pointer class22 ppt PC class22 ppt Modern view of a process read only code data 0 2 CS 213 S 01 A process with multiple threads Multiple threads can be associated with a process Process thread code data and kernel context Code and Data Thread main thread Thread 1 main thread shared libraries stack SP brk Data registers Condition codes Stack pointer SP Program counter PC run time heap PC stack 1 0 Kernel context 3 Thread 2 peer thread stack 2 run time heap read only code data VM structures Open files Signal handlers brk pointer class22 ppt Shared code and data shared libraries read write data Thread context Each thread has its own logical control flow sequence of PC val ues Each thread has its own stack Each thread shares the same code data and kernel context Each thread has its own thread id tid Thread 1 context read write data Thread 2 context Data registers Condition codes SP1 PC1 read only code data Data registers Condition codes SP2 PC2 0 Kernel context VM structures Open files Signal handlers brk pointer class22 ppt CS 213 S 01 Page 1 4 CS 213 S 01 Logical view of threads Concurrent thread execution Threads associated with a process form a pool of peers Two threads run concurrently are concurrent if their logical flows overlap in time Otherwise they are sequential unlike processes which form a tree hierarchy Threads associated with process foo Process hierarchy P0 T2 Examples T4 T1 P1 shared code data and kernel context sh T5 Thread A Thread B Thread C Concurrent A B A C Sequential B C sh Time sh T3 foo bar class22 ppt 5 class22 ppt CS 213 S 01 Threads vs processes Each has its own logical control flow Each can run concurrently Each is context switched Exception handler A handler can be viewed as a thread Waits for a signal from CPU Upon receipt executes some code then waits for next signal How threads and processes are different Threads share code and data processes typically do not Threads are somewhat less expensive than processes process control creating and reaping is twice as expensive asthread control Linux Pentium III numbers 20K cycles to create and reap a process 10K cycles to create and reap a thread 7 CS 213 S 01 Threads are a unifying abstraction for exceptional control flow How threads and processes are similar class22 ppt 6 Process A process is a thread shared code data and kernel context Signal handler A signal handler can be viewed as a thread Waits for a signal from the kernel or another process Upon receipt executes some code then waits for next signal class22 ppt CS 213 S 01 Page 2 8 CS 213 S 01 Posix threads Pthreads interface The Pthreads hello world program Pthreads Standard interface for 60 functions that manipulate threads from C programs hello c Pthreads hello world program include ics h Creating and reaping threads pthread create pthread join void thread void vargp Determining your thread ID pthread self 9 Thread arguments void p int main pthread t tid Terminating threads pthread cancel pthread exit exit terminates all threads ret terminates current thread Synchronizing access to shared variables pthread mutex init pthread mutex un lock pthread cond init pthread cond timed wait class22 ppt Thread attributes usually NULL Pthread create tid NULL thread NULL Pthread join tid NULL exit 0 return value void p thread routine void thread void vargp printf Hello world n return NULL class22 ppt CS 213 S 01 Execution of hello world 10 CS 213 S 01 Unix vs Posix error handling Unix style error handling Unix syscalls main thread if error return 1 and set errno variable to error code if OK return useful result as value 0 peer thread if pid wait NULL 0 perror wait exit 0 create peer thread print output wait for peer thread to terminate terminate thread via ret Posix style error handling newer Posix functions if error return nonzero error code zero if OK useful results are passed back in an argument exit terminates main thread and any peer threads class22 ppt 11 if rc pthread join tid retvalp 0 printf pthread create s n strerror rc exit 0 class22 ppt CS 213 S 01 Page 3 12 CS 213 S 01 Suggested error handling macros Pthreads wrappers We advocate Stevens s convention of providing wrappers for each system level function call Error checking crucial but cluttered Use these to simplify your error checking macro for unix style error handling define unix error msg do printf s s n msg strerror errno exit 0 while 0 wrapper function for pthread join void Pthread join pthread t tid void thread return int rc pthread join tid thread return if rc 0 posix error rc Pthread join macro for posix style error handling define posix error code msg do printf s s n msg strerror code exit 0 while 0 class22 ppt 13 class22 ppt CS 213 S 01 Basic thread control create a thread CS 213 S 01 int pthread join pthread t tid void thread return Creates a new peer thread Waits for a specific peer thread to terminate and then reaps it tidp thread id attrp thread attributes usually NULL routine thread routine argp input parameters to routine tid thread ID of thread to wait for thread return object returned by peer thread via ret stmt Akin to wait and wait pid but unlike wait Akin to fork Any thread can reap any other thread not just children Must wait for a specific thread no way to wait for any thread perceived by some as a flaw in the Pthreads design but without the confusing call once return twice semantics peer thread has local stack variables but shares all global variables class22 ppt 14 Basic thread control join int pthread create pthread t tidp pthread attr t attrp void routine void void argp wrapper is denoted by capitalizing first letter of function name wrapper has identical interface as the original function each wrapper does appropriate unix or posix style error checking wrapper typically returns nothing declutters code without compromising safety 15 class22 ppt CS 213 S 01 Page 4 16 CS 213 S 01 Linux implementation of Pthreads hellopid c Linux implements threads in an elegant way The


View Full Document

CMU CS 15213 - ConcurrencyI:Threads

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 ConcurrencyI:Threads
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 ConcurrencyI:Threads 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 ConcurrencyI:Threads 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?