Realizing Concurrency using Posix Threads (pthreads)IntroductionTopics to be CoveredObjectiveThreadsPthread LibraryCreating threadsUsing threadsThread’s local dataThread termination (destruction)Waiting for thread exitThe Thread ModelPer process vs per thread itemsMany Threads - One ProcessUser Level ThreadsKernel Level Threads“Green” ThreadsImplementing Threads in User SpaceImplementing Threads in the KernelHybrid ImplementationsScheduler ActivationsThread Scheduling (1)Thread Scheduling (2)Summary01/13/191Realizing Concurrency using Posix Threads (pthreads)B. Ramamurthy01/13/192IntroductionA thread refers to a thread of control flow: an independent sequence of execution of program code.Threads are powerful. As with most powerful tools, if they are not used appropriately thread programming may be inefficient.Thread programming has become viable solution for many problems with the advent of multiprocessors and client-server model of computing.Typically these problems are expected to handle many requests simultaneously. Example: multi-media, database applications, web applications.01/13/193Topics to be CoveredObjectiveWhat are Threads?POSIX threadsCreating threadsUsing threadsSummary01/13/194ObjectiveTo study POSIX standard for threads called Pthreads.To study thread control primitives for creation, termination, join, synchronization, concurrency, and scheduling.To learn to design multi-threaded applications.01/13/195ThreadsA thread is a unit of work to a CPU. It is strand of control flow.A traditional UNIX process has a single thread that has sole possession of the process’s memory and resources.Threads within a process are scheduled and execute independently.Many threads may share the same address space.Each thread has its own private attributes: stack, program counter and register context.01/13/196Pthread Librarya POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.API specifies behavior of the thread library, implementation is up to development of the library.Common in UNIX operating systems.Simply a collection of C function.01/13/197Creating threadsAlways include pthread library: #include <pthread.h>int pthread_create (pthread_t *tp, const pthread_attr_t * attr, void *(* start_routine)(void *), void *arg);This creates a new thread of control that calls the function start_routine.It returns a zero if the creation is successful, and thread id in tp (first parameter).attr is to modify the attributes of the new thread. If it is NULL default attributes are used.The arg is passing arguments to the thread function.01/13/198Using threads1. Declare a variable of type pthread_t2. Define a function to be executed by the thread.3. Create the thread using pthread_createMake sure creation is successful by checking the return value.4. Pass any arguments need through’ arg (packing and unpacking arg list necessary.)5. #include <pthread.h> at the top of your header.6. Compile: g++ file.c -lpthread -o executable01/13/199 Thread’s local dataVariables declared within a thread (function) are called local data.Local (static) data associated with a thread are allocated on the stack. So these may be deallocated when a thread returns. So don’t plan on using locally declared variables for returning arguments. Plan to pass the arguments thru argument list passed from the caller or initiator of the thread.01/13/1910Thread termination (destruction)Implicit : Simply returning from the function executed by the thread terminates the thread. In this case thread’s completion status is set to the return value.Explicit : Use thread_exit. Prototype: void thread_exit(void *status);The single pointer value in status is available to the threads waiting for this thread.01/13/1911Waiting for thread exitint pthread_join (pthread_t tid, void * *statusp);A call to this function makes a thread wait for another thread whose thread id is specified by tid in the above prototype.When the thread specified by tid exits its completion status is stored and returned in statusp.01/13/1912The Thread Model(a) Three processes each with one thread(b) One process with three threads01/13/1913Per process vs per thread itemsItems shared by all threads in a processItems private to each threadMany Threads - One ProcessThread 1Thread 2Low MemoryHigh MemoryTimeTCB 1TCB 2Second threadstarts hereFirst threadresumesUser Level ThreadsThreads first developed in user librariesOS unawareAny blocking call blocks entire process!Kernel Level ThreadsThreads recognized as usefulFunctions added to kernelBlocking call blocks only 1 threadSimplifies programming modelThreads can use multiple CPUsRequire interrupt for service“Green” ThreadsDifferent models made programming hardUser library intercepts all blocking callsmakes them non-blockingSupports same model for user & kernel level threads01/13/1918Implementing Threads in User SpaceA user-level threads package01/13/1919Implementing Threads in the KernelA threads package managed by the kernel01/13/1920Hybrid Implementations Multiplexing user-level threads onto kernel- level threads01/13/1921Scheduler ActivationsGoal – mimic functionality of kernel threadsgain performance of user space threadsAvoids unnecessary user/kernel transitionsKernel assigns virtual processors to each processlets runtime system allocate threads to processorsProblem: Fundamental reliance on kernel (lower layer) calling procedures in user space (higher layer)01/13/1922Thread Scheduling (1)Possible scheduling of user-level threads50-msec process quantumthreads run 5 msec/CPU burst01/13/1923Thread Scheduling (2)Possible scheduling of kernel-level threads50-msec process quantumthreads run 5 msec/CPU burst01/13/1924SummaryWe looked at thread-based concurrency.Pthread programmingImplementation of threads.We will look at a pthread programming demoStudy the details given in thread library
View Full Document