15213 Recitation Section C Shimin Chen 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 C 2 Shimin Chen 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 Web Browser 15213 Recitation C Web Server 3 Shimin Chen 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 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 15213 Recitation C 4 Shimin Chen 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 C PC read only code data 0 5 Shimin Chen Alternate 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 Descriptor table brk pointer 15213 Recitation C 6 Shimin Chen 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 C 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 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 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 pthread mutex init pthread mutex un lock pthread cond init pthread cond timed wait 15213 Recitation C 9 Shimin Chen 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 return value void p thread routine void thread void vargp printf Hello world n return NULL 15213 Recitation C Upper case Pthread xxx checks errors 10 Shimin Chen 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 C 11 Shimin Chen 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 C 12 Shimin Chen 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 C 13 Shimin Chen 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 14 Shimin Chen 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 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 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 C 17 Shimin Chen Shared Variables in Conceptual Model global variables are shared stack variables are private 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 C 0 Kernel context VM
View Full Document