15 213 The course that gives CMU its Zip Concurrency I Threads Nov 9 2000 Topics class22 ppt Thread concept Posix threads Pthreads interface Linux Pthreads implementation Concurrent execution Sharing data Traditional view of a process Process process context code data and stack Process context Program context Data registers Condition codes Stack pointer SP Program counter PC Kernel context VM structures Open files Signal handlers brk pointer class22 ppt Code data and stack stack SP shared libraries brk run time heap read write data PC read only code data 0 2 CS 213 F 00 Modern view of a process Process thread code data and kernel context Thread main thread Code and Data shared libraries stack SP brk Thread context run time heap read write data PC Data registers Condition codes Stack pointer SP Program counter PC read only code data 0 Kernel context VM structures Open files Signal handlers brk pointer class22 ppt 3 CS 213 F 00 A process with multiple threads Multiple threads can be associated with a process Each thread has its own logical control flow sequence of PC values Each thread shares the same code data and kernel context Each thread has its own thread id tid Thread 1 main thread Shared code and data Thread 2 peer thread shared libraries stack 1 run time heap read write data Thread 1 context Data registers Condition codes SP1 PC1 read only code data 0 Kernel context stack 2 Thread 2 context Data registers Condition codes SP2 PC2 VM structures Open files Signal handlers brk pointer class22 ppt 4 CS 213 F 00 Logical view of threads Threads associated with a process form a pool of peers unlike processes which form a tree hierarchy Threads associated with process foo Process hierarchy P0 T2 T4 T1 P1 shared code data and kernel context sh T5 T3 sh sh foo bar class22 ppt 5 CS 213 F 00 Concurrent thread execution Two threads run concurrently are concurrent if their logical flows overlap in time Otherwise they are sequential Examples Thread A Thread B Thread C Concurrent A B A C Sequential B C Time class22 ppt 6 CS 213 F 00 Threads vs processes How threads and processes are similar Each has its own logical control flow Each can run concurrently Each is context switched 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 as thread control Linux Pentium III numbers 20K cycles to create and reap a process 10K cycles to create and reap a thread class22 ppt 7 CS 213 F 00 Threads are a unifying abstraction for exceptional control flow 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 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 8 CS 213 F 00 Posix threads Pthreads interface Pthreads Standard interface for 60 functions that manipulate threads from C programs Creating and reaping threads pthread create pthread join Determining your thread ID pthread self 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 9 CS 213 F 00 The Pthreads hello world program hello c Pthreads hello world program include ics h Thread attributes usually NULL void thread void vargp Thread arguments void p int main pthread t tid Pthread create tid NULL thread NULL Pthread join tid NULL exit 0 thread routine void thread void vargp printf Hello world n return NULL class22 ppt 10 return value void p CS 213 F 00 Execution of hello world main thread peer thread create peer thread print output wait for peer thread to terminate terminate thread via ret exit terminates main thread and any peer threads class22 ppt 11 CS 213 F 00 Unix vs Posix error handling Unix style error handling Unix syscalls if error return 1 and set errno variable to error code if OK return useful result as value 0 if pid wait NULL 0 perror wait exit 0 Posix style error handling newer Posix functions if error return nonzero error code zero if OK useful results are passed back in an argument if rc pthread join tid retvalp 0 printf pthread create s n strerror rc exit 0 class22 ppt 12 CS 213 F 00 Suggested error handling macros 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 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 CS 213 F 00 Pthreads wrappers We advocate Steven s convention of providing wrappers for each system level function call 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 return nothing declutters code without compromising safety 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 class22 ppt 14 CS 213 F 00 int Basic thread control create a thread pthread create pthread t tidp pthread attr t attrp void routine void void argp Creates a new peer thread tidp thread id attrp thread attributes usually NULL routine thread routine argp input parameters to routine Akin to fork but without the confusing call once return twice semantics peer thread has local stack variables but shares all global variables class22 ppt 15 CS 213 F 00 Basic thread control join int pthread join pthread t tid void thread return Waits for a specific peer thread to terminate and then reaps it 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 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 class22 ppt 16 CS 213 F 00 Linux implementation of Pthreads Linux implements threads in an elegant way Threads are just processes that share the same kernel context fork creates a child
View Full Document