Thread 15213 S04 Recitation Section A Thread Memory Model Thread Interfaces System Calls Thread Safety Pitfalls of Using Thread Racing Semaphore Final Evaluation Forms pthread create pthread join pthread self pthread cancel pthread exit pthread mutex init pthread mutex un lock pthread cond init pthread cond timed wait 2 15 213 S 04 Why Do We Care About Thread Useful for L7 Part II A very important way to implement modern concurrent systems What s concurrency Web Server Web Browser Web Browser Web Browser 3 Proxy Web Server Web Server 15 213 S 04 Three Methods to Implement Concurrency 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 4 15 213 S 04 View of Process Process process context code data and stack Process context Program context Code data and stack stack SP Data registers Condition code Stack pointer SP Program counter PC Kernel context VM structures Descriptor table 5 shared libraries run time heap read write data PC read only code data 0 15 213 S 04 View of Thread Thread memory model Multiple threads can be associated with a process Each thread has its own logical control flow instruction flow Each thread has its own thread ID TID Each thread shares the same code data and kernel context 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 code SP1 PC1 6 0 Kernel context VM structures Descriptor table Thread 2 peer thread stack 2 Thread 2 context Data registers Condition code SP2 PC2 15 213 S 04 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 Synchronizing access to shared variables pthread mutex init pthread mutex un lock pthread cond init pthread cond timed wait 7 15 213 S 04 The Pthread hello world Program hello c Pthreads hello world program include csapp h thread routine void thread void vargp printf Hello world n return NULL int main pthread t tid Pthread create tid NULL thread NULL Pthread join tid NULL exit 0 8 15 213 S 04 Execution of Threaded hello world main thread Call Pthread create Pthread create returns peer thread call Pthread join main thread waits for peer thread to terminate printf return NULL peer thread terminates Pthread join returns exit terminates main thread and any peer threads 9 15 213 S 04 Practices Basic usage of thread Pthread exit exit Joinable and detached thread Thread safety 10 Protecting shared variable Function that returns a static pointer more 15 213 S 04 pthread exit exit Program 1 1 void thread void vargp pthread exit void 42 int main int i pthread t tid pthread create tid NULL thread NULL pthread join tid void i printf d n i 11 15 213 S 04 pthread exit exit Program 1 2 void thread void vargp exit 42 int main int i pthread t tid pthread create tid NULL thread NULL pthread join tid void i printf d n i 12 15 213 S 04 pthread exit exit pthread exit only terminates the current thread NOT the process Exit terminates all the threads in the process i e the process itself 13 15 213 S 04 Practices Basic usage of thread Pthread exit exit Joinable and detached thread Thread safety 14 Protecting shared variable Function that returns a static pointer more 15 213 S 04 Joinable 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 15 use pthread detach pthread self to make detached 15 213 S 04 Practices Basic usage of thread Pthread exit exit Joinable and detached thread Thread safety 16 Protecting shared variable Function that returns a static pointer more 15 213 S 04 Protecting shared variables Program 1 5 int i 42 void thread void vargp printf d n i void thread2 void vargp i 31 int main pthread t tid tid2 pthread create tid2 NULL thread2 void i pthread create tid NULL thread void i pthread join tid void i pthread join tid2 NULL 17 15 213 S 04 Practices Basic usage of thread Pthread exit exit Joinable and detached thread Thread safety 18 Protecting shared variable Function that returns a static pointer more 15 213 S 04 Functions that return a pointer to a static value int main struct in addr a a s addr inet addr 1 1 1 1 printf s n inet ntoa a Output 1 1 1 1 int main struct in addr a b a s addr inet addr 1 1 1 1 b s addr inet addr 2 2 2 2 printf s s n inet ntoa a inet ntoa b output 19 15 213 S 04 Thread Safety Class 1 Functions that do not protect shared variables Class 2 Functions that keep state across multiple invocations rand Textbook P 886 Class 3 Functions that return a pointer to a static variable Class 4 Functions that call thread unsafe functions 20 May or may not be thread unsafe Textbook P 887 15 213 S 04 More Practice Problems Program 1 3 1 4 are left for your practice 21 15 213 S 04 Racing A race occurs when the correctness of a program depends on one thread reaching point x in its control flow before another thread reaches point y Generally related with the access to the shared variables Need synchronization Ways to do synchronization 22 Semaphores Mutual exclusion 15 213 S 04 Program 2 b void foo void vargp int id id int vargp printf Thread d n id int main pthread t tid 2 int i Racing for i 0 i 2 i Pthread create tid i NULL foo i Pthread join tid 0 NULL Pthread join tid 1 NULL 23 15 213 S 04 Program 2 a void foo void vargp int myid myid int vargp Free vargp printf Thread d n myid int main pthread t tid 2 int i ptr Good for i 0 i 2 i ptr Malloc sizeof int ptr i Pthread create tid i 0 foo ptr Pthread join tid 0 0 Pthread join tid 1 0 24 15 213 S 04 Program 2 c void foo void vargp int id id int vargp printf Thread d n id int main pthread t tid 2 int i Good for i 0 i 2 i Pthread create tid i 0 foo i Pthread join tid 0 0 Pthread join tid 1 0 25 15 213 S 04 The general solution for …
View Full Document