Chapter 4: Threads: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts - 9 Edition
Chapter 4: Threads: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts - 9 Edition
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 4: Threads
Overview
Multicore Programming
Multithreading Models
Thread Libraries
Implicit Threading
Threading Issues
Operating System Examples
Operating System Concepts – 9th Edition 4.2 Silberschatz, Galvin and Gagne ©2013
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, Windows, and Java
thread libraries
To explore several strategies that provide implicit threading
To examine issues related to multithreaded programming
To cover operating system support for threads in Windows and
Linux
Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©2013
Thread
Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Motivation
Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Multithreaded Server Architecture
Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©2013
Benefits
Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Single and Multithreaded Processes
Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
User Threads and Kernel Threads
Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
User Threads and Kernel Threads
Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
Multithreading Models
One-to-One
Many-to-Many
Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Many-to-One
Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
One-to-One
Each user-level thread maps to kernel thread
Creating a user-level thread creates a kernel thread
More concurrency than many-to-one
Limitation
Number of threads per process sometimes
restricted due to overhead
Examples
Windows
Linux
Solaris 9 and later
Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model
Allows many user level threads to be
mapped to smaller or equal kernel
threads
Allows the operating system to create
a sufficient number of kernel threads
Solaris prior to version 9
Windows with the ThreadFiber
package
Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Two-level Model
Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
Thread Libraries
Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
Pthreads Example
Operating System Concepts – 9th Edition 4.17 Silberschatz, Galvin and Gagne ©2013
Pthreads Example
Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
Pthreads Example (Cont.)
Operating System Concepts – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
Pthreads Code for Joining Ten Threads
Operating System Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013
Windows Multithreaded C Program
Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Windows Multithreaded C Program (Cont.)
Operating System Concepts – 9th Edition 4.22 Silberschatz, Galvin and Gagne ©2013
Thread Cancellation
Terminating a thread before it has finished(database, web browser)
The thread to be canceled is referred to as target thread
Cancelation of a target thread may be handled using two general
approaches:
Asynchronous cancellation terminates the target thread
immediately
Deferred cancellation allows the target thread to periodically
check if it should be cancelled
The difficulty with cancellation occurs in situations where:
Resources have been allocated to a canceled thread.
A thread is canceled while in the midst of updating data it is
sharing with other threads.
Operating System Concepts – 9th Edition 4.23 Silberschatz, Galvin and Gagne ©2013
Thread Cancellation
The operating system usually will reclaim system resources from
a canceled thread but will not reclaim all resources.
Canceling a thread asynchronously does not necessarily free a
system-wide resource that is needed by others.
Deferred cancellation does not suffer from this problem:
One thread indicates that a target thread is to be canceled,
The cancellation occurs only after the target thread has
checked a flag to determine whether or not it should be
canceled.
The thread can perform this check at a point at which it can
be canceled safely.
Operating System Concepts – 9th Edition 4.24 Silberschatz, Galvin and Gagne ©2013
Pthread Cancellation
Operating System Concepts – 9th Edition 4.25 Silberschatz, Galvin and Gagne ©2013
Operating System Examples
Windows Threads
Linux Threads
Operating System Concepts – 9th Edition 4.26 Silberschatz, Galvin and Gagne ©2013
Windows Threads
Operating System Concepts – 9th Edition 4.27 Silberschatz, Galvin and Gagne ©2013
Windows Threads (Cont.)
Operating System Concepts – 9th Edition 4.28 Silberschatz, Galvin and Gagne ©2013
Windows Threads Data Structures
Operating System Concepts – 9th Edition 4.29 Silberschatz, Galvin and Gagne ©2013
Linux 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)
Flags control behavior
Operating System Concepts – 9th Edition 4.30 Silberschatz, Galvin and Gagne ©2013
End of Chapter 4
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013