Chapter 4: Threads. 4.2/23 Chapter 4: Threads Overview Multithreading Models.
Chapter 4: Threads. 2 Overview Multithreading Models Thread Libraries Threading Issues Operating...
-
date post
22-Dec-2015 -
Category
Documents
-
view
285 -
download
1
Transcript of Chapter 4: Threads. 2 Overview Multithreading Models Thread Libraries Threading Issues Operating...
Chapter 4: Chapter 4: ThreadsThreads
22
Chapter 4: ThreadsChapter 4: Threads
OverviewOverview
Multithreading ModelsMultithreading Models
Thread LibrariesThread Libraries
Threading IssuesThreading Issues
Operating System ExamplesOperating System Examples
Windows XP ThreadsWindows XP Threads
Linux ThreadsLinux Threads
33
ObjectivesObjectives
To introduce the notion of a thread — a To introduce the notion of a thread — a fundamental unit of CPU utilization that fundamental unit of CPU utilization that forms the basis of multithreaded computer forms the basis of multithreaded computer systemssystems
To discuss the APIs for the Pthreads, To discuss the APIs for the Pthreads, Win32, and Java thread librariesWin32, and Java thread libraries
To examine issues related to To examine issues related to multithreaded programmingmultithreaded programming
44
Single and Multithreaded Single and Multithreaded ProcessesProcesses
55
BenefitsBenefits
ResponsivenessResponsiveness
Resource SharingResource Sharing
EconomyEconomy
ScalabilityScalability
66
Multicore ProgrammingMulticore Programming
Multicore systems are putting pressure on Multicore systems are putting pressure on programmersprogrammers
Challenges includeChallenges include Dividing activitiesDividing activities BalanceBalance Data splittingData splitting Data dependencyData dependency Testing and debuggingTesting and debugging
77
Multithreaded Server Multithreaded Server ArchitectureArchitecture
88
Concurrent Execution on a Single-core Concurrent Execution on a Single-core SystemSystem
99
Parallel Execution on a Multicore Parallel Execution on a Multicore SystemSystem
1010
User ThreadsUser Threads
Thread management done by user-level Thread management done by user-level threads librarythreads library
Three primary thread libraries:Three primary thread libraries: POSIX POSIX PthreadsPthreads Win32 threadsWin32 threads Java threadsJava threads
1111
Kernel ThreadsKernel Threads
Supported by the KernelSupported by the Kernel
ExamplesExamples Windows XP/2000Windows XP/2000 SolarisSolaris LinuxLinux Tru64 UNIXTru64 UNIX Mac OS XMac OS X
1212
Chapter 4: ThreadsChapter 4: Threads
OverviewOverview
Multithreading ModelsMultithreading Models
Thread LibrariesThread Libraries
Threading IssuesThreading Issues
Operating System ExamplesOperating System Examples
Windows XP ThreadsWindows XP Threads
Linux ThreadsLinux Threads
1313
Multithreading ModelsMultithreading Models
Many-to-OneMany-to-One
One-to-OneOne-to-One
Many-to-ManyMany-to-Many
1414
Many-to-OneMany-to-One
Many user-level threads mapped to single Many user-level threads mapped to single kernel threadkernel thread
Examples:Examples: Solaris Green ThreadsSolaris Green Threads GNU Portable ThreadsGNU Portable Threads
1515
Many-to-One ModelMany-to-One Model
1616
One-to-OneOne-to-One
Each user-level thread maps to kernel Each user-level thread maps to kernel threadthread
ExamplesExamples Windows NT/XP/2000Windows NT/XP/2000 LinuxLinux Solaris 9 and laterSolaris 9 and later
1717
One-to-one ModelOne-to-one Model
1818
Many-to-Many ModelMany-to-Many Model
Allows many user level threads to Allows many user level threads to be mapped to many kernel threadsbe mapped to many kernel threads
Allows the operating system to Allows the operating system to create a sufficient number of kernel create a sufficient number of kernel threadsthreads
Solaris prior to version 9Solaris prior to version 9
Windows NT/2000 with the Windows NT/2000 with the ThreadFiberThreadFiber package package
1919
Many-to-Many ModelMany-to-Many Model
2020
Two-level ModelTwo-level ModelSimilar to M:M, except that it Similar to M:M, except that it allows a user thread to be allows a user thread to be boundbound to kernel thread to kernel thread
ExamplesExamples IRIXIRIX HP-UXHP-UX Tru64 UNIXTru64 UNIX Solaris 8 and earlierSolaris 8 and earlier
2121
Two-level ModelTwo-level Model
2222
Chapter 4: ThreadsChapter 4: Threads
OverviewOverview
Multithreading ModelsMultithreading Models
Thread LibrariesThread Libraries
Threading IssuesThreading Issues
Operating System ExamplesOperating System Examples
Windows XP ThreadsWindows XP Threads
Linux ThreadsLinux Threads
2323
Thread LibrariesThread Libraries
A A tthread hread librarylibrary provides programmer with provides programmer with an API for creating and managing threadsan API for creating and managing threads
Two primary ways of implementingTwo primary ways of implementing Library entirely in user spaceLibrary entirely in user space Kernel-level library supported by the OSKernel-level library supported by the OS
2424
PthreadsPthreads
May be provided either as user-level or kernel-May be provided either as user-level or kernel-levellevel
A POSIX standard (IEEE 1003.1c) API for A POSIX standard (IEEE 1003.1c) API for thread creation and synchronizationthread creation and synchronization
API specifies behavior of the thread library, API specifies behavior of the thread library, implementation is up to development of the implementation is up to development of the librarylibrary
Common in UNIX operating systems (Solaris, Common in UNIX operating systems (Solaris, Linux, Mac OS X)Linux, Mac OS X)
2525
Java ThreadsJava ThreadsJava threads are managed by the JVMJava threads are managed by the JVM
Typically implemented using the threads model Typically implemented using the threads model provided by underlying OSprovided by underlying OS
Java threads may be created by:Java threads may be created by: Extending Thread classExtending Thread class Implementing the Runnable interfaceImplementing the Runnable interface
2626
Chapter 4: ThreadsChapter 4: Threads
OverviewOverview
Multithreading ModelsMultithreading Models
Thread LibrariesThread Libraries
Threading IssuesThreading Issues
Operating System ExamplesOperating System Examples
Windows XP ThreadsWindows XP Threads
Linux ThreadsLinux Threads
2727
Threading IssuesThreading IssuesSemantics of Semantics of fork()fork() and and exec()exec() system callssystem calls
Thread cancellation Thread cancellation of of target threadtarget thread Asynchronous or deferredAsynchronous or deferred
Signal Signal handlinghandling
Thread poolsThread pools
Thread-specific dataThread-specific data
Scheduler activationsScheduler activations
2828
Semantics of fork() and exec()Semantics of fork() and exec()
Does Does fork()fork() duplicate only the calling duplicate only the calling thread or all threads?thread or all threads?
2929
Thread CancellationThread CancellationTerminating a thread before it has Terminating a thread before it has finishedfinished
Two general approaches:Two general approaches: Asynchronous cancellationAsynchronous cancellation
terminates the target thread terminates the target thread immediatelyimmediately
Deferred cancellationDeferred cancellation allows the allows the target thread to periodically check if target thread to periodically check if it should be cancelledit should be cancelled
3030
Signal HandlingSignal HandlingSignals are used in UNIX systems to notify a Signals are used in UNIX systems to notify a process that a particular event has occurredprocess that a particular event has occurred
A A signal handler signal handler is used to process signalsis used to process signals1.1. Signal is generated by particular eventSignal is generated by particular event
2.2. Signal is delivered to a processSignal is delivered to a process
3.3. Signal is handledSignal is handled
3131
Signal HandlingSignal Handling
Options for the signal handler:Options for the signal handler: Deliver the signal to the thread to which the Deliver the signal to the thread to which the
signal appliessignal applies Deliver the signal to every thread in the Deliver the signal to every thread in the
processprocess Deliver the signal to certain threads in the Deliver the signal to certain threads in the
processprocess Assign a specific thread to receive all signals Assign a specific thread to receive all signals
for the processfor the process
3232
Thread PoolsThread Pools
Create a number of threads in a pool Create a number of threads in a pool where they await workwhere they await work
Advantages:Advantages: Usually slightly faster to service a request Usually slightly faster to service a request
with an existing thread than create a new with an existing thread than create a new threadthread
Allows the number of threads in the Allows the number of threads in the application(s) to be bound to the size of application(s) to be bound to the size of the poolthe pool
3333
Thread Specific DataThread Specific Data
Allows each thread to have its own Allows each thread to have its own copy of datacopy of data
Useful when you do not have Useful when you do not have control over the thread creation control over the thread creation process (i.e., when using a thread process (i.e., when using a thread pool)pool)
3434
Scheduler ActivationsScheduler ActivationsBoth M:M and Two-level models require Both M:M and Two-level models require communication to maintain the appropriate communication to maintain the appropriate number of kernel threads allocated to the number of kernel threads allocated to the applicationapplication
Scheduler activations provide Scheduler activations provide upcalls upcalls - a - a communication mechanism from the kernel communication mechanism from the kernel to the thread libraryto the thread library
This communication allows an application This communication allows an application to maintain the correct number kernel to maintain the correct number kernel threadsthreads
3535
Chapter 4: ThreadsChapter 4: Threads
OverviewOverview
Multithreading ModelsMultithreading Models
Thread LibrariesThread Libraries
Threading IssuesThreading Issues
Operating System ExamplesOperating System Examples
Windows XP ThreadsWindows XP Threads
Linux ThreadsLinux Threads
3636
Operating System ExamplesOperating System Examples
Windows XP ThreadsWindows XP Threads
Linux ThreadLinux Thread
3737
Chapter 4: ThreadsChapter 4: Threads
OverviewOverview
Multithreading ModelsMultithreading Models
Thread LibrariesThread Libraries
Threading IssuesThreading Issues
Operating System ExamplesOperating System Examples
Windows XP ThreadsWindows XP Threads
Linux ThreadsLinux Threads
3838
Windows XP ThreadsWindows XP Threads
3939
Windows XP ThreadsWindows XP ThreadsImplements the one-to-one mapping, kernel-Implements the one-to-one mapping, kernel-levellevel
Each thread containsEach thread contains A thread idA thread id Register setRegister set Separate user and kernel stacksSeparate user and kernel stacks Private data storage areaPrivate data storage area
The register set, stacks, and private storage The register set, stacks, and private storage area are known as the area are known as the context context of the threadsof the threads
4040
Windows XP ThreadsWindows XP Threads
The primary data structures of a thread The primary data structures of a thread include:include: ETHREAD (executive thread block)ETHREAD (executive thread block) KTHREAD (kernel thread block)KTHREAD (kernel thread block) TEB (thread environment block)TEB (thread environment block)
4141
Chapter 4: ThreadsChapter 4: Threads
OverviewOverview
Multithreading ModelsMultithreading Models
Thread LibrariesThread Libraries
Threading IssuesThreading Issues
Operating System ExamplesOperating System Examples
Windows XP ThreadsWindows XP Threads
Linux ThreadsLinux Threads
4242
Linux ThreadsLinux Threads
4343
Linux ThreadsLinux ThreadsLinux refers to them as Linux refers to them as taskstasks rather than rather than threadsthreads
Thread creation is done through Thread creation is done through clone()clone() system call system call
clone()clone() allows a child task to allows a child task to share the address space of the share the address space of the parent task (process)parent task (process)
4444
End of Chapter 4End of Chapter 4