Download presentation
Presentation is loading. Please wait.
1
Chapter 4: Threads
2
Chapter 4: Threads Overview Multithreading Models Thread Libraries
Threading Issues Operating System Examples Windows XP Threads Linux Threads
3
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
4
Single and Multithreaded Processes
5
Benefits Responsiveness Resource Sharing Economy Scalability
6
Multicore Programming
Multicore CPU appears as multiple CPUs to OS Multithreading makes more efficient use of it
7
Multicore Programming
Multicore systems putting pressure on programmers, challenges include Dividing activities Balance Data splitting Data dependency Testing and debugging
8
Types of Threads User threads Running user-level code
Managed by user process Needs kernel thread to run system calls, I/O operations, etc. Can only be scheduled on CPU through kernel thread Kernel threads Running kernel-level code Managed by kernel Can be scheduled on CPU A user process needs both user threads and kernel threads to run
9
Multithreading Models
User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads Kernel Threads Supported by the Kernel Present in practically all OS Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X Relationship between user and kernel threads Many-to-One One-to-One Many-to-Many
10
Many-to-One Many user-level threads mapped to single kernel thread
Examples: Solaris Green Threads GNU Portable Threads
11
Many-to-One Model
12
One-to-One Each user-level thread maps to kernel thread Examples
Windows NT/XP/2000 Linux Solaris 9 and later
13
One-to-one Model
14
Many-to-Many Model Allows many user level threads to be mapped to fewer or equal kernel threads Number of kernel threads can vary per system, and per process Allows the operating system to create a sufficient number of kernel threads Windows NT/2000 with the ThreadFiber package
15
Many-to-Many Model
16
Two-level Model Variation of Many-to-Many, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier
17
Two-level Model
18
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 We’ll study three implementations POSIX Pthread Win32 Java
19
POSIX Pthreads POSIX standard (IEEE c) defines specifications for an API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library May be provided either as user-level or kernel-level Common in UNIX operating systems (Solaris, Linux, Mac OS X) Implemented in C in pthread.h pthread_t pthread_attr_t pthread_create() pthread_join() pthread_exit()
20
Win32 Threads Very similar to Pthreads
Function prototypes in windows.h CreateThread() WaitForSingleObject() CloseHandle() Implementation in proprietary Microsoft code inside Windows kernel
21
Java Threads Java threads are managed by the JVM
Typically implemented using the threads model provided by underlying OS Java threads may be created by: Extending Thread class Implementing the Runnable interface run() function is thread’s executable code Thread start() function creates thread, calls run() Thread exits automatically at the end of run() public interface Runnable { public abstract void run(); }
22
Implicit Threading Instead of giving developer control through thread libraries, have threads handled by compilers and run-time libraries OpenMP Compiler directives for C/C++ Programmer inserts preprocessor tags Compiler isolates parallelizable sections and creates as many threads as there are CPUs Grand Central Dispatch Apple-specific Programmer inserts tags in code (like OpenMP) GCD manages POSIX threads, assigns work in three FIFO priority queues
23
Threading Issues Semantics of fork() and exec() system calls
Thread cancellation of target thread Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations
24
Semantics of fork() and exec()
Recall: fork() and exec() functions in Unix handle new processes Process creation, replace process code When a process has several threads, and one thread calls these functions, does it affect all threads or just the calling one?
25
Thread Cancellation Terminating a target thread before it has finished
Two general approaches: Asynchronous cancellation terminates the target thread immediately from another thread Deferred cancellation allows the target thread to periodically check if it should cancel itself
26
Signal Handling Signals are used to notify a process that a particular event has occurred Signal is generated by particular event (typically interrupts) Signal is delivered to a process Signal is handled A signal handler is used to process signals Default handlers in kernel can be overwritten by user-defined handler How to deliver a process-specific signal in a multithreaded process? Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process
27
Thread Pools So far, we’re creating threads when needed
Thread creation/termination overhead With no maximum thread number, more and more threads can be created until they exhaust system resources Create a number of threads at process creation in a thread pool, where they await work and return after work is done 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 Part of the Win32 API QueueUserWorkItem()
28
Thread Specific Data One advantage of threads is no duplication of common process data But some threads need thread-specific data Data unique to that thread Data that did not exist at process creation time We need support for thread-specific data Included in Pthreads, Win32 and Java
29
Scheduler Activations
Many-to-Many and Two-Level models have a set of kernel threads and user threads Need to keep a balance in the number of each – for best performance, should be adjusted dynamically Require communication between kernel and thread library Systems with these models often put a light-weight process between kernel thread and user thread, to work as a virtual CPU for the user This allows scheduler activation kernel-process communication scheme Kernel provides a set of LWP to process Process schedules its own user threads on LWP When a thread waits for an event, the kernel warns the process with an upcall and allocates a new LWP to process Process runs upcall handler on new LWP to save thread state, lose thread’s LWP, and keeps new LWP to schedule other threads
30
Operating System Examples
Windows XP Threads Linux Thread 30
31
Windows XP Threads Implements the one-to-one mapping, kernel-level
Each thread contains A thread id Register set Separate user and kernel stacks, for when running in user or kernel mode Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (origin & parent) KTHREAD (kernel thread information) TEB (user thread information)
32
Windows XP Threads
33
Linux Threads Linux does not distinguish between processes and threads, calls them all tasks Task creation is done through clone() system call, with a set of flags Each task is represented by a kernel structure task_struct, which only contains pointers to other data structures instead of storing task info Makes varying levels of sharing between tasks possible
34
Review What’s the difference between a program, a process and a thread? What are the four advantages of multithreading? How does creating a new thread differ from creating a new process? In general, not for Linux tasks…
35
Exercises Read the entire chapter
If you have the “with Java” textbook, skip the Java sections and subtract 1 to the following section numbers 4.2 4.3 4.4 4.8 4.16 4.18
36
End of Chapter 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.