chapter 4: threads

44
Chapter 4: Chapter 4: Threads Threads

Upload: joie

Post on 06-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

Chapter 4: Threads. Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads. Objectives. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 4:  Threads

Chapter 4: Chapter 4: ThreadsThreads

Page 2: Chapter 4:  Threads

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

Page 3: Chapter 4:  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

Page 4: Chapter 4:  Threads

44

Single and Multithreaded Single and Multithreaded ProcessesProcesses

Page 5: Chapter 4:  Threads

55

BenefitsBenefits

ResponsivenessResponsiveness

Resource SharingResource Sharing

EconomyEconomy

ScalabilityScalability

Page 6: Chapter 4:  Threads

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

Page 7: Chapter 4:  Threads

77

Multithreaded Server Multithreaded Server ArchitectureArchitecture

Page 8: Chapter 4:  Threads

88

Concurrent Execution on a Single-core Concurrent Execution on a Single-core SystemSystem

Page 9: Chapter 4:  Threads

99

Parallel Execution on a Multicore Parallel Execution on a Multicore SystemSystem

Page 10: Chapter 4:  Threads

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

Page 11: Chapter 4:  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

Page 12: Chapter 4:  Threads

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

Page 13: Chapter 4:  Threads

1313

Multithreading ModelsMultithreading Models

Many-to-OneMany-to-One

One-to-OneOne-to-One

Many-to-ManyMany-to-Many

Page 14: Chapter 4:  Threads

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

Page 15: Chapter 4:  Threads

1515

Many-to-One ModelMany-to-One Model

Page 16: Chapter 4:  Threads

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

Page 17: Chapter 4:  Threads

1717

One-to-one ModelOne-to-one Model

Page 18: Chapter 4:  Threads

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

Page 19: Chapter 4:  Threads

1919

Many-to-Many ModelMany-to-Many Model

Page 20: Chapter 4:  Threads

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

Page 21: Chapter 4:  Threads

2121

Two-level ModelTwo-level Model

Page 22: Chapter 4:  Threads

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

Page 23: Chapter 4:  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

Page 24: Chapter 4:  Threads

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)

Page 25: Chapter 4:  Threads

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

Page 26: Chapter 4:  Threads

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

Page 27: Chapter 4:  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

Page 28: Chapter 4:  Threads

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?

Page 29: Chapter 4:  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

Page 30: Chapter 4:  Threads

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

Page 31: Chapter 4:  Threads

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

Page 32: Chapter 4:  Threads

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

Page 33: Chapter 4:  Threads

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)

Page 34: Chapter 4:  Threads

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

Page 35: Chapter 4:  Threads

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

Page 36: Chapter 4:  Threads

3636

Operating System ExamplesOperating System Examples

Windows XP ThreadsWindows XP Threads

Linux ThreadLinux Thread

Page 37: Chapter 4:  Threads

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

Page 38: Chapter 4:  Threads

3838

Windows XP ThreadsWindows XP Threads

Page 39: Chapter 4:  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

Page 40: Chapter 4:  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)

Page 41: Chapter 4:  Threads

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

Page 42: Chapter 4:  Threads

4242

Linux ThreadsLinux Threads

Page 43: Chapter 4:  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)

Page 44: Chapter 4:  Threads

4444

End of Chapter 4End of Chapter 4