Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/os08_CH04.pdf ·...
Transcript of Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/os08_CH04.pdf ·...
Chapter 4: Multithreaded Chapter 4: Multithreaded ProgrammingProgramming
4.2 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Chapter 4: Multithreaded Chapter 4: Multithreaded ProgrammingProgramming
Overview
Multithreading Models
Thread Libraries
Threading Issues
Operating-System Examples
4.3 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
OverviewOverview
A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack.
A traditional, or heavyweight process has a single thread of control.
It shares with other threads belonging to the same process its code section, data section, and other OS resources, such as open files and signals.
In busy WWW server: The server creates a separate thread that would listen for clients requests, when a request was made, creates a thread to service the request.
4.4 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Single and Multithreaded ProcessesSingle and Multithreaded Processes
4.5 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Benefits (1)Benefits (1)
Responsiveness: Allow a program to continue running even if part of it is blocked or is performing a lengthy operation.
Resource sharing: several different threads of activity all within the same address space.
Economy: Allocating memory and resources for process creation is costly. In Solaris, creating a process is about 30 times slower than is creating a thread, and context switching is about five times slower. A register set switch is still required, but no no memorymemory--management related work is neededmanagement related work is needed.
4.6 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Benefits (2)Benefits (2)
Utilization of multiprocessor architecture: Several thread may be running in parallel on different processors.
Of course, multithreading a process may introduce concurrency control problem that requires the use of critical sections or locks.
4.7 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
User ThreadsUser Threads
Thread management done by user-level threads library.
Fast: All thread creating and scheduling are done in user space without the need for kernel intervention.
Any user-level thread performing a blocking system call will cause the entire process to block, even if there are other threads available to run within the applications, if the kernel is single-thread.
Examples
Pthreads
Win32 threads
4.8 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Kernel ThreadsKernel Threads
Supported and managed directly by the OS
Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
4.9 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Multithreading Models (1)Multithreading Models (1)
(Multi-Thread vs. Multi-process)
Multiple processes
Each is independent and has it own program counter, stack register, and address space. This is useful for unrelated jobs.
Multiple processes can perform the same task as well. (e.g., provide data to remote machines in a network file system). Each executes the same code but has it own memory and file resources.
Multiple-thread process
It is more efficient to have one process containing multiple threads serve the same task.
Most Systems Support for both user and kernel threads
4.10 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Multithreading Models (2)Multithreading Models (2) Uses fewer resources, including memory, open files and CPU scheduling.
Threads are not independent to each other. This structure does not provide protection.
Only a single user can own an individual task with multiple threads. The threads should be designed to assist one another.
Threads can create child threads. If one thread is blocked, another thread can run.
Threads provide a mechanism that allows sequential processes to make blocking system calls while also achieving parallelism.
4.11 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Multithreading ModelsMultithreading Models
Many-to-One
One-to-One
Many-to-Many
4.12 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
ManyMany--toto--OneOne
Many user-level threads mapped to single kernel thread.
Used on systems that do not support kernel threads.
Thread management is done in user space, so it is efficient.
The entire process will block if a thread makes a blocking system call.
Only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors.
4.13 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
ManyMany--toto--One ModelOne Model
4.14 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
OneOne--toto--OneOne
Each user-level thread maps to kernel thread.
More concurrency
Overhead: Creating a thread requires creating the corresponding kernel thread.
Examples
- Windows XP/NT/2000
- Linux
- Solaris 9 and later
4.15 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
OneOne--toto--one Modelone Model
4.16 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
ManyMany--toto--Many ModelMany 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
Multiplexes many user-level threads to a smaller or equal number of kernel threads
The corresponding kernel threads can run in parallel on a multiprocessor.
When a thread performs a blocking call, the kernel can schedule another thread for execution
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
4.17 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
ManyMany--toto--Many ModelMany Model
4.18 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
TwoTwo--level Modellevel Model
Similar to M:M, except that it allows a user thread to be bound to kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
4.19 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
TwoTwo--level Modellevel Model
4.20 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Threading IssuesThreading Issues
Semantics of fork() and exec() system calls. Duplicate all the threads or not?
Thread cancellation: Asynchronous or deferred
Signal handling: Where then should a signal be delivered?
Thread pools: Create a number of threads at process startup.
Thread specific data: Each thread might need its own copy of certain data.
Scheduler activations
4.21 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Semantics of fork() and exec()Semantics of fork() and exec()
Does fork() duplicate only the calling thread or all threads?
Two versions of fork(): duplicates all threads and another duplicates only the thread that invoked the fork() system call
If exec() is called immediately after forking, then duplicating all threads is unnecessary.
4.22 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Thread CancellationThread Cancellation
Terminating a thread before it has completed.
Two general approaches:
Asynchronous cancellation terminates the target thread immediately
Deferred cancellation The target thread to periodically check whether it should be terminate, allowing it an opportunity to terminate itself in an orderly fashion (canceled safely).
Cancellation points
4.23 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Signal HandlingSignal Handling
Signals are used in UNIX systems to notify a process that a particular event has occurred
A signal handler is used to process signals
Signal is generated by particular event
Signal is delivered to a process
Signal is handled
Delivering Signals:
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
4.24 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Thread PoolsThread 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
4.25 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Thread Specific DataThread Specific Data
Allows each thread to have its own copy of data
Each transaction assigned a unique number in the transaction-processing system
Useful when you do not have control over the thread creation process (i.e., when using a thread pool)
4.26 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Scheduler ActivationsScheduler Activations
Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application
Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library
This communication allows an application to maintain the correct number kernel threads
4.27 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
OperatingOperating--system Examplesystem Example
Explore how threads are implemented in Windows XP and Linux systems.
4.28 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Windows XP ThreadsWindows XP Threads
Implements the one-to-one mapping
Each thread contains
A thread id
Register set
Separate user and kernel stacks
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 (executive thread block)
KTHREAD (kernel thread block)
TEB (thread environment block)
4.29 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Linux ThreadsLinux Threads
Linux refers to them as tasks rather than threads
Thread creation is done through clone() system call
clone() allows a child task to share the address space of the parent task (process)
4.30 Silberschatz, Galvin and Gagne ©2005Operating System Concepts
Home worksHome works
1, 3, 4, 5, 6
Project- Matrix Multiplication (multi-thread vs. multi-process)
End of Chapter 4End of Chapter 4