Concurrency Anubhav Gupta Dec 2 2002 Outline Threads Synchronization Thread safety of Library Functions Important Dates Lab 7 Proxy due on Thursday Dec 5 Final Exam Tuesday Dec 17 15213 Recitation 2 Anubhav Gupta Concurrent Servers Iterative servers can only serve one client at a time Concurrent servers are able to handle multiple requests in parallel Required by L7 Part II Web Server Web Browser Web Browser Proxy Web Browser 15213 Recitation Web Server Web Server 3 Anubhav Gupta Three Ways for Creating Concurrent Servers 1 Processes Fork a child process for every incoming client connection Difficult to share data among child processes 2 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 scheduler 3 Threads Create a thread to handle every incoming client connection Our focus today 15213 Recitation 4 Anubhav Gupta 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 Code data and stack stack SP shared libraries brk run time heap read write data Kernel context VM structures Descriptor table brk pointer 15213 Recitation PC read only code data 0 5 Anubhav Gupta Alternate View of a Process Process thread code data and kernel context Thread main thread Code and Data shared libraries SP stack 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 Descriptor table brk pointer 15213 Recitation 6 Anubhav Gupta A Process With Multiple Threads Multiple threads can be associated with a process Each thread has its own logical control flow instruction flow 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 shared libraries run time heap read write data read only code data stack 1 Thread 1 context Data registers Condition codes SP1 PC1 15213 Recitation 0 Kernel context VM structures Descriptor table brk pointer 7 Thread 2 peer thread stack 2 Thread 2 context Data registers Condition codes SP2 PC2 Anubhav Gupta 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 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 15213 Recitation 8 Anubhav Gupta 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 exit terminates all threads return terminates current thread Synchronizing access to shared variables 15213 Recitation pthread mutex init pthread mutex un lock pthread cond init pthread cond timed wait 9 Anubhav Gupta The Pthreads hello world Program hello c Pthreads hello world program include csapp 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 15213 Recitation return value void p Upper case Pthread xxx checks errors 10 Anubhav Gupta Execution of Threaded hello world main thread call Pthread create Pthread create returns peer thread call Pthread join printf main thread waits for peer thread to terminate return NULL peer thread terminates Pthread join returns exit terminates main thread and any peer threads 15213 Recitation 11 Anubhav Gupta Thread Based Concurrent Echo Server int main int argc char argv int listenfd connfdp port clientlen struct sockaddr in clientaddr pthread t tid if argc 2 fprintf stderr usage s port n argv 0 exit 0 port atoi argv 1 listenfd open listenfd port while 1 clientlen sizeof clientaddr connfdp Malloc sizeof int connfdp Accept listenfd SA clientaddr clientlen Pthread create tid NULL thread connfdp 15213 Recitation 12 Anubhav Gupta Thread Based Concurrent Server cont thread routine void thread void vargp int connfd int vargp Pthread detach pthread self Free vargp echo r connfd thread safe version of echo Close connfd return NULL 15213 Recitation 13 Anubhav Gupta Issue 1 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 use pthread detach pthread self to make detached 15213 Recitation 14 Anubhav Gupta Issue 2 Avoid Unintended Sharing connfdp Malloc sizeof int connfdp Accept listenfd SA clientaddr clientlen Pthread create tid NULL thread connfdp For example what happens if we pass the address of connfd to the thread routine as in the following code connfd Accept listenfd SA clientaddr clientlen Pthread create tid NULL thread void connfd 15213 Recitation 15 Anubhav Gupta Issue 3 Thread safe Easy to share data structures between threads But we need to do this correctly Recall the shell lab Job data structures Shared between main process and signal handler Need ways to synchronize multiple control of flows 15213 Recitation 16 Anubhav Gupta Threads Memory Model Conceptual model Each thread runs in the context of a 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 15213 Recitation 17 Anubhav Gupta Caveats of Conceptual Models All global variables are shared All static local variables are shared In practice any thread can read and write the stack of any other thread So one can use a global pointer to point to a stack variable Then all threads can access the stack variable But this is not a good programming practice More about this in this Thu s lecture 15213 Recitation 18 Anubhav Gupta Sharing With
View Full Document