0% found this document useful (0 votes)
17 views23 pages

Ch4-Threads 2021 NHV - Key

Chapter 4 discusses threads as fundamental units of CPU utilization in multithreaded systems, covering various threading models, libraries, and issues in multithreaded programming. It highlights the benefits of multithreading, such as responsiveness and resource sharing, and examines user and kernel threads along with examples from Windows XP and Linux. The chapter also addresses threading issues like cancellation, signal handling, and thread-specific data.

Uploaded by

ngocthi20072004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views23 pages

Ch4-Threads 2021 NHV - Key

Chapter 4 discusses threads as fundamental units of CPU utilization in multithreaded systems, covering various threading models, libraries, and issues in multithreaded programming. It highlights the benefits of multithreading, such as responsiveness and resource sharing, and examines user and kernel threads along with examples from Windows XP and Linux. The chapter also addresses threading issues like cancellation, signal handling, and thread-specific data.

Uploaded by

ngocthi20072004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Chapter 4: Threads

Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009

Chapter 4: Threads

‣ Overvie
‣ Multithreading Model
‣ Thread Librarie
‣ Threading Issue
‣ Operating System Example
‣ Windows XP Thread
‣ Linux Threads

Operating System Concepts – 8th Edition 2 Silberschatz, Galvin and Gagne ©2009
w

Objectives

‣ To introduce the notion of a thread — a fundamental unit


of CPU utilization that forms the basis of multithreaded
computer system
‣ To discuss the APIs for the Pthreads, Win32, and Java
thread librarie
‣ To examine issues related to multithreaded
programming

Operating System Concepts – 8th Edition 3 Silberschatz, Galvin and Gagne ©2009

Motivation
‣ Threads run within applicatio
‣ Multiple tasks with the application can be implemented by
separate thread
✦ Update displa
✦ Fetch dat
✦ Spell checkin
✦ Answer a network reques

‣ Process creation is heavy-weight while thread creation is light-


weigh
‣ Can simplify code, increase efficienc
‣ Kernels are generally multithreaded

Operating System Concepts – 8th Edition 4 Silberschatz, Galvin and Gagne ©2009
t

Single and Multithreaded Processes

Operating System Concepts – 8th Edition 5 Silberschatz, Galvin and Gagne ©2009

Multithreaded Server Architecture

Operating System Concepts – 8th Edition 6 Silberschatz, Galvin and Gagne ©2009
Benefits

‣ Responsiveness

‣ Resource Sharing

‣ Economy

‣ Scalability

Operating System Concepts – 8th Edition 7 Silberschatz, Galvin and Gagne ©2009

Multicore Programming

‣ Multicore systems putting pressure on programmers,


challenges include
✦ Dividing activitie
✦ Balanc
✦ Data splittin
✦ Data dependenc
✦ Testing and debugging

Operating System Concepts – 8th Edition 8 Silberschatz, Galvin and Gagne ©2009
e

Concurrent Execution on a
Single-core System

Operating System Concepts – 8th Edition 9 Silberschatz, Galvin and Gagne ©2009

Parallel Execution on a
Multicore System

Operating System Concepts – 8th Edition 10 Silberschatz, Galvin and Gagne ©2009
User Threads

‣ Thread management done by user-level threads library

‣ Three primary thread libraries


✦ POSIX Pthreads
✦ Win32 thread
✦ Java threads

Operating System Concepts – 8th Edition 11 Silberschatz, Galvin and Gagne ©2009

Kernel Threads

‣ Supported by the Kernel

‣ Example
✦ Windows XP/200
✦ Solari
✦ Linu
✦ Tru64 UNI
✦ Mac OS X

Operating System Concepts – 8th Edition 12 Silberschatz, Galvin and Gagne ©2009
x

Multithreading Models

‣ Many-to-One

‣ One-to-One

‣ Many-to-Many

Operating System Concepts – 8th Edition 13 Silberschatz, Galvin and Gagne ©2009

Many-to-One

‣ Many user-level threads mapped to single kernel threa

‣ Examples
✦ Solaris Green Thread
✦ GNU Portable Threads

Operating System Concepts – 8th Edition 14 Silberschatz, Galvin and Gagne ©2009
:

Many-to-One Model

Operating System Concepts – 8th Edition 15 Silberschatz, Galvin and Gagne ©2009

One-to-One

‣ Each user-level thread maps to kernel threa

‣ Example
✦ Windows NT/XP/200
✦ Linu
✦ Solaris 9 and later

Operating System Concepts – 8th Edition 16 Silberschatz, Galvin and Gagne ©2009
x

One-to-one Model

Operating System Concepts – 8th Edition 17 Silberschatz, Galvin and Gagne ©2009

Many-to-Many Model

‣ Allows many user level threads to be mapped to


many kernel thread

‣ Allows the operating system to create a sufficient


number of kernel thread

‣ Solaris prior to version

‣ Windows NT/2000 with the ThreadFiber package

Operating System Concepts – 8th Edition 18 Silberschatz, Galvin and Gagne ©2009
s

Many-to-Many Model

Operating System Concepts – 8th Edition 19 Silberschatz, Galvin and Gagne ©2009

Two-level Model

‣ Similar to M:M, except that it allows a user thread to be


bound to kernel threa

‣ Example
✦ IRI
✦ HP-U
✦ Tru64 UNI
✦ Solaris 8 and earlier

Operating System Concepts – 8th Edition 20 Silberschatz, Galvin and Gagne ©2009
X

Two-level Model

Operating System Concepts – 8th Edition 21 Silberschatz, Galvin and Gagne ©2009

Thread Libraries

‣ Thread library provides programmer with API for


creating and managing thread

‣ Two primary ways of implementin


✦ Library entirely in user spac
✦ Kernel-level library supported by the OS

Operating System Concepts – 8th Edition 22 Silberschatz, Galvin and Gagne ©2009
e

Pthreads

‣ May be provided either as user-level or kernel-leve

‣ A POSIX standard (IEEE 1003.1c) API for thread


creation and synchronizatio

‣ API specifies behavior of the thread library,


implementation is up to development of the librar

‣ Common in UNIX operating systems (Solaris, Linux,


Mac OS X)

Operating System Concepts – 8th Edition 23 Silberschatz, Galvin and Gagne ©2009

Pthreads Example

Operating System Concepts – 8th Edition 24 Silberschatz, Galvin and Gagne ©2009
n

Pthreads Example (Cont.)

Operating System Concepts – 8th Edition 25 Silberschatz, Galvin and Gagne ©2009

Win32 API Multithreaded C Program

Operating System Concepts – 8th Edition 26 Silberschatz, Galvin and Gagne ©2009
Win32 API Multithreaded C Program (Cont.)

Operating System Concepts – 8th Edition 27 Silberschatz, Galvin and Gagne ©2009

Java Threads

‣ Java threads are managed by the JV

‣ Typically implemented using the threads model


provided by underlying O

‣ Java threads may be created by:

✦ Extending Thread clas


✦ Implementing the Runnable interface

Operating System Concepts – 8th Edition 28 Silberschatz, Galvin and Gagne ©2009
s

Java Multithreaded Program

Operating System Concepts – 8th Edition 29 Silberschatz, Galvin and Gagne ©2009

Java Multithreaded Program (Cont.)

Operating System Concepts – 8th Edition 30 Silberschatz, Galvin and Gagne ©2009
Threading Issues

‣ Semantics of fork() and exec() system call

‣ Thread cancellation of target threa


✦ Asynchronous or deferre

‣ Signal handlin
✦ Synchronous and asynchronous

Operating System Concepts – 8th Edition 31 Silberschatz, Galvin and Gagne ©2009

Threading Issues (Cont.)

‣ Thread pool

‣ Thread-specific dat
✦ Create Facility needed for data private to thread
‣ Scheduler activations

Operating System Concepts – 8th Edition 32 Silberschatz, Galvin and Gagne ©2009
s

Semantics of fork() and exec()

‣ Does fork() duplicate only the calling thread or all


threads?

Operating System Concepts – 8th Edition 33 Silberschatz, Galvin and Gagne ©2009

Thread Cancellation

‣ Terminating a thread before it has finishe

‣ Two general approaches


✦ Asynchronous cancellation terminates the target
thread immediately
✦ Deferred cancellation allows the target thread to
periodically check if it should be cancelled.

Operating System Concepts – 8th Edition 34 Silberschatz, Galvin and Gagne ©2009
.

Signal Handling
‣ Signals are used in UNIX systems to notify a process that a
particular event has occurred
‣ A signal handler is used to process signal
1. Signal is generated by particular even
2. Signal is delivered to a proces
3. Signal is handle

‣ Options
✦ Deliver the signal to the thread to which the signal applie
✦ Deliver the signal to every thread in the proces
✦ Deliver the signal to certain threads in the proces
✦ Assign a specific thread to receive all signals for the
process

Operating System Concepts – 8th Edition 35 Silberschatz, Galvin and Gagne ©2009

Thread Pools

‣ Create a number of threads in a pool where they await


wor

‣ Advantages
✦ Usually slightly faster to service a request with an
existing thread than create a new threa
✦ Allows the number of threads in the application(s) to
be bound to the size of the pool

Operating System Concepts – 8th Edition 36 Silberschatz, Galvin and Gagne ©2009
k

Thread Specific Data

‣ Allows each thread to have its own copy of dat

‣ Useful when you do not have control over the thread


creation process (i.e., when using a thread pool)

Operating System Concepts – 8th Edition 37 Silberschatz, Galvin and Gagne ©2009

Scheduler Activations

‣ Both M:M and Two-level models require


communication to maintain the appropriate number of
kernel threads allocated to the applicatio

‣ Scheduler activations provide upcalls - a


communication mechanism from the kernel to the
thread librar

‣ This communication allows an application to maintain


the correct number kernel threads

Operating System Concepts – 8th Edition 38 Silberschatz, Galvin and Gagne ©2009
y

Lightweight Processes

Operating System Concepts – 8th Edition 39 Silberschatz, Galvin and Gagne ©2009

Operating System Examples

‣ Windows XP Thread

‣ Linux Thread

Operating System Concepts – 8th Edition 40 Silberschatz, Galvin and Gagne ©2009
s

Windows XP Threads
‣ Implements the one-to-one mapping, kernel-leve
‣ Each thread contain
✦ A thread i
✦ Register se
✦ Separate user and kernel stack
✦ Private data storage are

‣ The register set, stacks, and private storage area are known as
the context of the thread
‣ The primary data structures of a thread include
✦ ETHREAD (executive thread block
✦ KTHREAD (kernel thread block
✦ TEB (thread environment block)

Operating System Concepts – 8th Edition 41 Silberschatz, Galvin and Gagne ©2009

Windows XP Threads Data Structures

Operating System Concepts – 8th Edition 42 Silberschatz, Galvin and Gagne ©2009
d

Linux Threads
‣ Provides fork() and clone() system call
✦ fork(): traditional functionality of duplicating a
process
✦ clone(): create thread and allows a child task to

share the address space of the parent task


(process
‣ Doesn’t distinguish between process and thread; uses
the term task —rather than process or thread
‣ Unique kernel data structure(struct task_struct)
exists for each task in the system

Operating System Concepts – 8th Edition 43 Silberschatz, Galvin and Gagne ©2009

Linux Threads
‣ Data of new tas
✦ fork(): new task is created; copy of all the associated data
structures of the parent process
✦ clone():new task is created and points to the data structures
of the parent task, depending on the set of flags passed to clone()
✦ if none of these flags is set: no sharing takes place, functionality
similar to that provided by the fork() system call

Operating System Concepts – 8th Edition 44 Silberschatz, Galvin and Gagne ©2009

End of Chapter 4

Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009

You might also like