Thread 15213-S04, Recitation, Section ASlide 2Why Do We Care About ThreadThree Methods to Implement ConcurrencyView of ProcessView of ThreadPosix Threads (Pthreads) InterfaceThe Pthread "hello, world" ProgramExecution of Threaded“hello, world”Practicespthread_exit() & exit()Slide 12Slide 13Slide 14Joinable & Detached ThreadsSlide 16Protecting shared variablesSlide 18Functions that return a pointer to a static valueThread SafetyMore Practice ProblemsRacingProgram 2.bProgram 2.aProgram 2.cThe general solution for racingProgram 2.dProgram 2.eSummary So FarFinalEvaluation FormsThreadThread15213-S04, Recitation, Section A15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore Final & Evaluation Forms– 2 –15-213, S’04pthread_createpthread_joinpthread_selfpthread_cancelpthread_exitpthread_mutex_initpthread_mutex_[un]lockpthread_cond_initpthread_cond_[timed]wait– 3 –15-213, S’04Why Do We Care About ThreadUseful for L7 Part IIA very important way to implement modern concurrent systemsWhat’s concurrency?WebBrowserWebServerWebBrowserWebBrowserWebServerWebServerProxy– 4 –15-213, S’04Three Methods to Implement Concurrency1. ProcessesFork a child process for every incoming client connectionDifficult to share data among child processes2. ThreadsCreate a thread to handle every incoming client connectionOur focus today3. I/O multiplexing with Unix select()Use select() to notice pending socket activity Manually interleave the processing of multiple open connectionsMore complex! ~ implementing your own app-specific thread package!– 5 –15-213, S’04View of ProcessProcess = process context + code, data, and stackshared librariesrun-time heap0read/write dataProgram context: Data registers Condition code Stack pointer (SP) Program counter (PC)Kernel context: VM structures Descriptor tableCode, data, and stackread-only code/datastackSPPCProcess context– 6 –15-213, S’04View of ThreadMultiple threads can be associated with a processEach 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 contextshared librariesrun-time heap0read/write data Shared code and dataread-only code/dataThread 1 context: Data registers Condition code SP1 PC1stack 1Thread 1 (main thread)Kernel context: VM structures Descriptor tableThread 2 context: Data registers Condition code SP2 PC2stack 2Thread 2 (peer thread)Thread memorymodel– 7 –15-213, S’04Posix Threads (Pthreads) InterfaceStandard interface for ~60 functionsCreating and reaping threads.pthread_createpthread_joinDetermining your thread IDpthread_selfTerminating threadspthread_cancelpthread_exitSynchronizing access to shared variablespthread_mutex_initpthread_mutex_[un]lockpthread_cond_initpthread_cond_[timed]wait– 8 –15-213, S’04The 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);}– 9 –15-213, S’04Execution of Threaded“hello, world”main threadmain thread waits for peer thread to terminateexit() terminates main thread and any peer threadspeer threadCall Pthread_create()call Pthread_join()Pthread_join() returnsprintf()return NULL;(peer threadterminates)Pthread_create() returns– 10 –15-213, S’04PracticesBasic usage of threadPthread_exit() & exit()Joinable and detached threadThread safetyProtecting shared variableFunction that returns a static pointer(more) ……– 11 –15-213, S’04pthread_exit() & exit()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);}Program 1.1– 12 –15-213, S’04pthread_exit() & exit()Program 1.2void *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);}– 13 –15-213, S’04pthread_exit() & exit()pthread_exit() only terminates the current thread, NOT the processExit() terminates all the threads in the process, i.e., the process itself– 14 –15-213, S’04PracticesBasic usage of threadPthread_exit() & exit()Joinable and detached threadThread safetyProtecting shared variableFunction that returns a static pointer(more) ……– 15 –15-213, S’04Joinable & Detached ThreadsAt 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.– 16 –15-213, S’04PracticesBasic usage of threadPthread_exit() & exit()Joinable and detached threadThread safetyProtecting shared variableFunction that returns a static pointer(more) ……– 17 –15-213, S’04Protecting shared variablesProgram 1.5int 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);}– 18 –15-213, S’04PracticesBasic usage of threadPthread_exit() & exit()Joinable and detached threadThread safetyProtecting shared variableFunction that returns a static pointer(more) ……– 19 –15-213, S’04Functions that return a pointer to a static valueint 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: ???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– 20 –15-213, S’04Thread SafetyClass 1: Functions that do not protect shared variablesClass 2: Functions that keep state across
View Full Document