Chapter 4 Threads
Chapter 4 Threads
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 4: Threads
Overview
Multicore Programming
Multithreading Models
Thread Libraries
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
Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©2013
Terminology
Multiprogramming
A computer running more than one program at a time (like running
Excel and Firefox simultaneously)
Context switching
Multiprocessing
A computer using more than one CPU (processor) or core at a time▪
Multitasking
Multitasking is a logical extension of multi programming (time
sharing)
Tasks sharing a common resource (like 1 CPU)
Multithreading
Thread (a code segments)
is an extension of multitasking
Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Motivation
Most modern applications are multithreaded
Example:
Web browser: one thread displays images or text while another thread
receives data from the network
Word processor: a thread for displaying the graphics, another one for
responding to keystrokes, and a third thread for performing spelling and
grammar checking.
Threads run within application
Multiple tasks with the application can be implemented by separate
threads
Update display
Fetch data
Spell checking
Answer a network request
Process creation is heavy-weight while thread creation is light-weight
Can simplify code, increase efficiency
Kernels are generally multithreaded
Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Two Process Characteristics
Two process characteristics are treated independently by the operating
system
The unit of execution (CPU utilization) is referred to as a thread or
lightweight process.
The unit of resource ownership is referred to as a process or task.
Two process characteristics:
Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©2013
One or More Threads in a Process
Each thread has:
An execution state (Running, Ready, etc.)
A saved thread context when not running
An execution stack
Some per-thread static storage for local variables
Access to the memory and resources of its processes, shared with all other
threads in that process.
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
Two Process Characteristics
A single thread of execution per = instruction trace
process, in which the concept of a
thread is not recognized, is
referred to as a single-threaded
approach
o MS-DOS is an example
one processes one processes
one thread multiple threads
The right half of Figure 4.1 depicts
multithreaded approaches
A Java run-time environment is an
example of a system of one
process with multiple threads
Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
Key Benefits of Threads
Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
Multicore Programming
Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
Concurrent execution on single-core system:
Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Multicore Programming (Cont.)
Types of parallelism
Data parallelism – distributes subsets of the same data
across multiple cores, same operation on each
Task parallelism – distributing threads across cores, each
thread performing unique operation
As # of threads grows, so does architectural support for threading
CPUs have cores as well as hardware threads
Consider Oracle SPARC T4 with 8 cores, and 8 hardware
threads per core
Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
Data and Task parallelism
Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
Threads Implementation
Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
User Threads and Kernel Threads
User threads - management done by user-level threads library
o The user library contains code for creating threads, destroying threads,
scheduling thread execution and …
o The kernel is not aware of the existence of threads
Three primary thread libraries:
POSIX Pthreads
Windows threads
Java threads
Kernel threads - Supported by the Kernel
o There is no thread management code in the application level, simply an
application programming interface (API) to the kernel thread facility
o The kernel maintains context information for the process and threads
Examples – virtually all general purpose operating systems, including:
Windows Linux
Solaris Tru64 UNIX
Mac OS X
Operating System Concepts – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
Advantages of ULTs and KLTs
Advantages of User Level Threads(ULTs)
Scheduling can be application specific and specify by programmer
ULTs can run on any OS, even the ones that do not support
multithreading like embedded OSs.
Less overhead: Thread switching does not require kernel mode
privileges
Advantages of Kernel Level Threads(KLTs)
The kernel can simultaneously schedule multiple threads from the
same process on multiple processors
If one thread in a process is blocked, the kernel can schedule
another thread of the same process
Kernel routines themselves can be multithreaded (in all modern
OSs)
Operating System Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013
Disadvantages of ULTs and KLTs
Disadvantages of User Level Threads(ULTs)
In a typical OS many system calls are blocking
As a result, when a ULT executes a system call, not only is that
thread blocked, but all of the threads within the process are blocked
as well
In a pure ULT strategy, a multithreaded application cannot take
advantage of multiprocessing
A kernel assigns one process to only one processor at a time,
therefore, only a single thread within a process can execute at a
time
Disadvantage of Kernel Level Threads(KLTs)
The transfer of control from one thread to another within the same
process requires a mode switch to the kernel (increase overhead for
OS
Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Multithreading Models
Operating System Concepts – 9th Edition 4.22 Silberschatz, Galvin and Gagne ©2013
Many-to-One
Operating System Concepts – 9th Edition 4.23 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
Number of threads per process sometimes
restricted due to overhead
Examples
Windows
Linux
Solaris 9 and later
Operating System Concepts – 9th Edition 4.24 Silberschatz, Galvin and Gagne ©2013
Ex: Multithreaded Server Architecture
Operating System Concepts – 9th Edition 4.25 Silberschatz, Galvin and Gagne ©2013
Many-to-Many 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
Solaris prior to version 9
Windows with the ThreadFiber
package
Operating System Concepts – 9th Edition 4.26 Silberschatz, Galvin and Gagne ©2013
Two-level Model
Operating System Concepts – 9th Edition 4.27 Silberschatz, Galvin and Gagne ©2013
Thread Libraries
Operating System Concepts – 9th Edition 4.28 Silberschatz, Galvin and Gagne ©2013
Pthreads
Operating System Concepts – 9th Edition 4.29 Silberschatz, Galvin and Gagne ©2013
Pthreads Example
Operating System Concepts – 9th Edition 4.30 Silberschatz, Galvin and Gagne ©2013
Pthreads Example (Cont.)
Operating System Concepts – 9th Edition 4.31 Silberschatz, Galvin and Gagne ©2013
Pthreads Code for Joining 10 Threads
Operating System Concepts – 9th Edition 4.32 Silberschatz, Galvin and Gagne ©2013
Windows Multithreaded C Program
Operating System Concepts – 9th Edition 4.33 Silberschatz, Galvin and Gagne ©2013
Windows Multithreaded C Program (Cont.)
Operating System Concepts – 9th Edition 4.34 Silberschatz, Galvin and Gagne ©2013
Java Threads
Operating System Concepts – 9th Edition 4.35 Silberschatz, Galvin and Gagne ©2013
Java Multithreaded Program
Operating System Concepts – 9th Edition 4.36 Silberschatz, Galvin and Gagne ©2013
End of Chapter 4
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013