Carnegie Mellon Introduction to Computer Systems 15 213 18 243 spring 2009 26th Lecture Apr 28th Instructors Gregory Kesden and Markus P schel Carnegie Mellon Don t Miss Your Chance me A B Carnegie Mellon Today Threads basics Synchronization Races deadlocks thread safety Carnegie Mellon Process Traditional View Process process context code data and stack Code data and stack stack Process context Program context Data registers Condition codes Stack pointer SP Program counter PC Kernel context VM structures Descriptor table brk pointer SP shared libraries brk run time heap read write data read only code data PC 0 Carnegie Mellon Process Alternative View Process thread code data and kernel context Code data and kernel context Thread Program context Data registers Condition codes Stack pointer SP Program counter PC shared libraries brk run time heap read write data read only code data PC 0 SP stack Kernel context VM structures Descriptor table brk pointer Carnegie Mellon Process with Two Threads Thread 1 Program context Data registers Condition codes Stack pointer SP Program counter PC brk stack PC SP shared libraries run time heap read write data read only code data 0 Thread 2 Program context Data registers Condition codes Stack pointer SP Program counter PC SP Code data and kernel context stack Kernel context VM structures Descriptor table brk pointer Carnegie Mellon Threads vs Processes Threads and processes similarities Each has its own logical control flow Each can run concurrently with others Each is context switched scheduled by the kernel Threads and processes differences Threads share code and data processes typically do not Threads are much less expensive than processes Process control creating and reaping is more expensive as thread control Context switches for processes much more expensive than for threads Carnegie Mellon Threads vs Processes contd Processes form a tree hierarchy Threads form a pool of peers Each thread can kill any other Each thread can wait for any other thread to terminate Main thread first thread to run in a process Process hierarchy Thread pool T2 P0 T4 T1 P1 sh sh shared code data and kernel context sh T5 foo T3 Carnegie Mellon Posix Threads Pthreads Interface Pthreads Standard interface for 60 functions that manipulate threads from C programs Threads run thread routines void threadroutine void vargp Creating and reaping threads pthread create pthread t tid func f void arg pthread join pthread t tid void thread return Determining your thread ID pthread self Terminating threads pthread cancel pthread t tid pthread exit void tread return return in primary thread routine terminates the thread exit terminates all threads Synchronizing access to shared variables Carnegie Mellon The Pthreads Hello world Program hello c Pthreads hello world program include csapp h Thread attributes usually NULL void thread void vargp int main pthread t tid Thread arguments void p Pthread create tid NULL thread NULL Pthread join tid NULL exit 0 thread routine void thread void vargp printf Hello world n return NULL assigns return value void p Carnegie Mellon Detaching Threads Thread based servers Use detached threads to avoid memory leaks 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 use pthread detach pthread self to make detached Must be careful to avoid unintended sharing For example what happens if we pass the address of connfd to the thread routine Pthread create tid NULL thread void connfd Carnegie Mellon Pros and Cons of Thread Based Designs Easy to share data structures between threads e g logging information file cache Threads are more efficient than processes Unintentional sharing can introduce subtle and hard toreproduce errors The ease with which data can be shared is both the greatest strength and the greatest weakness of threads Carnegie Mellon Today Threads basics Synchronization Races deadlocks thread safety Carnegie Mellon Shared Variables in Threaded C Programs Question Which variables in a threaded C program are shared variables The answer is not as simple as global variables are shared and stack variables are private Requires answers to the following questions What is the memory model for threads How are variables mapped to each memory instance How many threads might reference each of these instances Carnegie Mellon Threads Memory Model Conceptual model Multiple threads run within the context of a single process Each thread has its own separate thread context Thread ID stack stack pointer program counter condition codes and general purpose registers All threads share the remaining process context Code data heap and shared library segments of the process virtual address space Open files and installed handlers Operationally this model is not strictly enforced Register values are truly separate and protected but Any thread can read and write the stack of any other thread Mismatch between the conceptual and operation model is a source of confusion and errors Carnegie Mellon Thread Accessing Another Thread s Stack char ptr global int main int i pthread t tid char msgs 2 Hello from foo Hello from bar ptr msgs for i 0 i 2 i Pthread create tid NULL thread void i Pthread exit NULL thread routine void thread void vargp int myid int vargp static int svar 0 printf d s svar d n myid ptr myid svar Peer threads access main thread s stack indirectly through global ptr variable Carnegie Mellon Mapping Variables to Memory Instances Global var 1 instance ptr data Local vars 1 instance i m msgs m char ptr global int main int i pthread t tid char msgs 2 Hello from foo Hello from bar ptr msgs for i 0 i 2 i Pthread create tid NULL thread void i Pthread exit NULL Local var 2 instances myid p0 peer thread 0 s stack myid p1 peer thread 1 s stack thread routine void thread void vargp int myid int vargp static int svar 0 printf d s svar d n myid ptr myid svar Local static var 1 instance svar data Carnegie Mellon Shared Variable Analysis Which variables are shared Variable instance Referenced by main thread Referenced by peer thread 0 Referenced by peer thread 1 ptr yes yes yes svar no yes yes i m yes no no msgs m yes yes yes myid p0 no yes no myid p1 no no yes Answer A variable x is shared iff multiple threads
View Full Document