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