Chapter 4: Threads 14.1 Silberschatz, Galvin and Gagne 2009
Chapter 4: Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples 14.2 Silberschatz, Galvin and Gagne 2009
Objectives To introduce the notion of a thread a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To discuss the APIs for the Pthreads, Win32, and Java thread libraries To examine issues related to multithreaded programming 14.3 Silberschatz, Galvin and Gagne 2009
Single and Multithreaded Processes 14.4 Silberschatz, Galvin and Gagne 2009
Benefits Responsiveness Resource Sharing Economy Scalability 14.5 Silberschatz, Galvin and Gagne 2009
Multithreaded Server Architecture 14.7 Silberschatz, Galvin and Gagne 2009
User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads 14.10 Silberschatz, Galvin and Gagne 2009
Kernel Threads Managed by the Kernel, user space <- -> kernel space switching costly Examples Windows XP/2000 Linux Mac OS X 14.11 Silberschatz, Galvin and Gagne 2009
Multithreading Models Many-to-One One-to-One Many-to-Many 14.12 Silberschatz, Galvin and Gagne 2009
Many-to-One Many user-level threads mapped to single kernel thread Used on the kernels that do not support multithreading 14.13 Silberschatz, Galvin and Gagne 2009
One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later 14.15 Silberschatz, Galvin and Gagne 2009
Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Windows NT/2000 with the ThreadFiber package 14.17 Silberschatz, Galvin and Gagne 2009
Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS 14.21 Silberschatz, Galvin and Gagne 2009
Pthreads May be provided either as user-level or kernel-level A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization Common in UNIX operating systems (Solaris, Linux, Mac OS X) 14.22 Silberschatz, Galvin and Gagne 2009
Pthreads Example All Pthreads programs must include the pthread.h header file. The statement pthread_t tid declares the identifier for the thread we will create. Each thread has a set of attributes, including stack size and scheduling information. A separate thread is created with the pthread_create() function call.
Pthreads Example the program has two threads: the initial (or parent) thread in main() and the summation (or child) thread performing the summation operation in the runner() function. This program follows the fork-join strategy described earlier: after creating the summation thread, the parent thread will wait for it to terminate by calling the pthread join() function. The summation thread will terminate when it calls the function pthread exit(). Once the summation thread has returned, the parent thread will output the value of the shared data sum.
Pthreads Example
Pthreads Example (Cont.)
Windows threads The technique for creating threads using the Windows thread library is similar to the Pthreads technique in several ways. Threads are created in the Windows API using the CreateThread() function, a set of attributes for the thread is passed to this function. We illustrate the Windows thread API in the C program. Notice that we must include the windows.h header file when using the Windows API. NOTE: the DWORD data type is an unsigned 32-bit integer
Windows threads Recall that the POSIX Pthread program had the parent thread wait for the summation thread using the pthread join() statement. We perform the equivalent of this in the Windows API using the WaitForSingleObject() function, which causes the creating thread to block until the summation thread has exited. In situations that require waiting for multiple threads to complete, the WaitForMultipleObjects() function is used.
Win32 threads Example
Win32 API Multithreaded C Program (Cont.)
Threading Issues Thread cancellation of target thread Asynchronous or deferred Thread pools 14.30 Silberschatz, Galvin and Gagne 2009
Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled 14.32 Silberschatz, Galvin and Gagne 2009
Thread Pools Create a number of threads in a pool where they await work. Advantages: Usually slightly faster to service a request with an existing thread than create a new thread. Allows the number of threads in the application(s) to be bound to the size of the pool. 14.34 Silberschatz, Galvin and Gagne 2009