15213 Recitation Section C Shimin Chen Dec 2 2002 Important Dates Lab 7 Proxy due on Thursday Dec 5 Final Exam Tuesday Dec 17 Outline Threads Synchronization Thread safety of Library Functions 15213 Recitation C Concurrent Servers Shimin Chen Three Ways for Creating 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 Browser Web Server Web Browser Web Server Proxy 2 1 Processes Fork a child process for every incoming client connection Difficult to share data among child processes 2 Threads Create a thread to handle every incoming client connection Our focus today 3 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 thread package Web Browser 15213 Recitation C Web Server 3 Shimin Chen 15213 Recitation C 4 Shimin Chen 1 Traditional View of a Process Process process context code data and stack Process context Alternate View of a Process Process thread code data and kernel context Thread main thread Code data and stack SP Program context Data registers Condition codes Stack pointer SP Program counter PC shared libraries brk 5 Shimin Chen 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 Shared code and data shared libraries run time heap read write data read only code data stack 1 Thread 1 context 15213 Recitation C read only code data 0 Kernel context VM structures Descriptor table brk pointer 15213 Recitation C A Process With Multiple Threads Data registers Condition codes SP1 PC1 PC Data registers Condition codes Stack pointer SP Program counter PC read only code data run time heap read write data 0 15213 Recitation C Thread 1 main thread brk Thread context run time heap read write data PC stack SP shared libraries Kernel context VM structures Descriptor table brk pointer Code and Data stack 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 Shimin Chen 6 Shimin Chen 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 15213 Recitation C 8 Shimin Chen 2 Posix Threads Pthreads Interface Standard interface for 60 functions Creating and reaping threads hello c Pthreads hello world program include csapp h pthread create pthread join pthread self pthread cancel pthread exit exit terminates all threads return terminates current thread Synchronizing access to shared variables 9 Shimin Chen Execution of Threaded hello world 15213 Recitation C 10 Shimin Chen if argc 2 fprintf stderr usage s port n argv 0 exit 0 port atoi argv 1 printf main thread waits for peer thread to terminate Upper case Pthread xxx checks errors Thread Based Concurrent Echo Server peer thread call Pthread join return value void p int main int argc char argv int listenfd connfdp port clientlen struct sockaddr in clientaddr pthread t tid main thread call Pthread create Pthread create returns Pthread create tid NULL thread NULL Pthread join tid NULL exit 0 thread routine void thread void vargp printf Hello world n return NULL pthread mutex init pthread mutex un lock pthread cond init pthread cond timed wait return NULL peer thread terminates 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 Pthread join returns exit terminates main thread and any peer threads 15213 Recitation C Thread arguments void p int main pthread t tid Terminating threads 15213 Recitation C Thread attributes usually NULL void thread void vargp Determining your thread ID The Pthreads hello world Program 11 Shimin Chen 15213 Recitation C 12 Shimin Chen 3 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 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 Why should we use detached threads pthread join blocks the calling thread 15213 Recitation C 13 Shimin Chen 15213 Recitation C 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 C 15 Shimin Chen 14 Shimin Chen 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 C 16 Shimin Chen 4 Threads Memory Model Shared Variables in Conceptual 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 C 17 Shimin Chen global variables are shared stack variables are private 15213 Recitation C 19 Shimin Chen 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 C Caveats of Conceptual
View Full Document