Chapter 4: Threads &
Concurrency
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
Overview
Multicore Programming
Multithreading Models
Thread Libraries
Implicit Threading
Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018
Objectives
Identify the basic components of a thread, and contrast threads
and processes
Describe the benefits and challenges of designng
multithreaded applications
Illustrate different approaches to implicit threading including
thread pools, fork-join, and Grand Central Dispatch
Describe how the Windows and Linux operating systems
represent threads
Design multithreaded applications using the Pthreads, Java,
and Windows threading APIs
Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018
Motivation
Most modern applications are multithreaded
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 – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018
Motivation
So far we assumed a process was an executing program with a single
thread of control.
If a process has multiple threads of control, it can perform more than
one task at a time.
Most operating system kernels are now multithreaded.
Examples:
• A multithreaded word processor could, for example, assign one thread to
manage user input while another thread runs the spell checker.
• Multithreaded Server Architecture: A web server accepts client requests
for Web pages, images, sound, etc.
Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018
Threads Concept
A thread is a basic unit of CPU utilization. It comprises:
• a thread ID,
• a program counter,
• a register set,
• and a stack.
It shares with other threads belonging to the same process its:
• code section,
• data section,
• and other operating-system resources, such as open files and
signals.
Figure 4.1 illustrates the difference between a traditional heavyweight
single-threaded process and a multithreaded process.
Operating System Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018
Single and Multithreaded Processes
Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018
Benefits of Multithreading
it allows a program to continue running even if part of it is blocked
or performing a lengthy operation (e.g., web-browser loading an
Responsiveness image); and hence increasing responsiveness to the user.
This is useful in designing user interfaces.
Processes can only share resources through techniques such
as shared memory and message passing which must be
Resource explicitly arranged by the programmer. However, threads share
Sharing resources of process by default.
This feature allows an application to have several threads of
activity within the same address space.
Process creation is costly. Because threads share the resources of
Economy the process to which they belong, it is more economical to create
and context-switch threads.
(Utilizing multiprocessor architectures) – same process can
Scalability make use of several CPUs by using threads
Operating System Concepts – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
Multicore Programming
Multicore or multiprocessor systems putting pressure on
programmers, challenges include:
1. Dividing activities: find areas that can be divided into separate,
concurrent tasks and thus can run in parallel on individual cores.
2. Balance: ensure tasks perform equal work of equal value.
3. Data splitting: . Just as applications are divided into separate tasks, the
data accessed by the tasks must be divided to run on separate cores.
4. Data dependency: When one task depends on data from another,
programmers must ensure that the execution of the tasks is synchronized
to accommodate the data dependency.
5. Testing and debugging: is more difficult than for single-threaded
applications since there are many different execution paths when a
program is running in parallel on multiple cores
Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallelism
A system is parallel if it can perform more than one task
simultaneously.
A concurrent system supports more than one task by allowing all the
tasks to make progress.
Concurrent execution on single-core system:
Parallelism on
a multi-core system:
Q It is possible to have concurrency without
Operating System Concepts – 10 Edition
th
parallelism.
4.10 Silberschatz, Galvin and Gagne ©2018
Multicore Programming
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
Operating System Concepts – 10th Edition 4.11 Silberschatz, Galvin and Gagne ©2018
User Threads and Kernel Threads
User threads – are supported above the kernel and are managed
without kernel support. The management done by a user-level threads
library. Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
Kernel threads - are supported and managed (scheduled) directly by
the operating system (Kernel). Virtually, all general -purpose operating
systems support kernel threads. These include:
• Windows
• Linux
• Mac OS X
• iOS
• Android
Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018
User and Kernel Threads
Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018
Many-to-One Model
Many user-level threads are mapped to a single kernel thread
(+) Thread management is done by the thread library in user space, so it is
efficient.
(-) One thread blocking causes all other threads to be blocked,
[Link] entire process will block if a thread makes a blocking system call.
(-) Multiple threads may not run in parallel on muti-core system because only one
thread may be in kernel at a time
Few systems currently use this model:
• Solaris Green Threads
• GNU Portable Threads
Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018
One-to-One Model
Each user thread is mapped to a kernel thread
(+) It provides more concurrency than many-to-one by allowing another thread
to run when a thread makes a blocking system call.
(+) It also allows multiple threads to run in parallel on multiprocessors.
(-) Creating a user thread requires creating a corresponding kernel thread
(-) Number of threads supported by the system is usually restricted due to
overhead of creating kernel threads
Examples
• Windows
• Linux
• Solaris 9 and later
Operating System Concepts – 10th Edition 4.16 Silberschatz, Galvin and Gagne ©2018
Many-to-Many Model
Allows many user threads to be mapped to many (a smaller or equal
number of) kernel threads
(+) Allows the operating system to create a sufficient number of kernel
threads
(+) It overcome all previous models shortcomings regarding concurrency,
i.e. developers can create as many user threads as necessary, and the
corresponding kernel threads can run in parallel on a multiprocessor. Also,
when a thread performs a blocking system call, the kernel can schedule
another thread for execution.
Example: Windows with the ThreadFiber package
Operating System Concepts – 10th Edition 4.17 Silberschatz, Galvin and Gagne ©2018
Two-level Model
Similar to M:M, except that it allows a user thread to be bound to a
kernel thread
Example: Solaris 8 and earlier
Operating System Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018
Thread Libraries
Thread library provides the programmer with an API for creating and
managing threads.
Two primary ways of implementing a thread library:
• Library entirely in user space
• Kernel-level library supported by the OS
Three main thread libraries are in use today: POSIX Pthreads,
Windows, and Java.
Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018
Pthreads
May be provided either as user-level or kernel-level
A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
This API is a specification for thread behavior, not an
implementation. OS designers may implement the specification in
any way they wish
Common in UNIX operating systems (Solaris, Linux & Mac OS X)
Operating System Concepts – 10th Edition 4.20 Silberschatz, Galvin and Gagne ©2018
Pthreads Example
Operating System Concepts – 10th Edition 4.21 Silberschatz, Galvin and Gagne ©2018
Pthreads Example (Cont.)
Operating System Concepts – 10th Edition 4.22 Silberschatz, Galvin and Gagne ©2018
Pthreads Code for Joining 10 Threads
Operating System Concepts – 9 th Edition 4. 21 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 10th Edition 4.23 Silberschatz, Galvin and Gagne ©2018
Windows Multithreaded C Program
Operating System Concepts – 10th Edition 4.24 Silberschatz, Galvin and Gagne ©2018
Windows Multithreaded C Program (Cont.)
Operating System Concepts – 10th Edition 4.25 Silberschatz, Galvin and Gagne ©2018
Java Threads
Java threads are managed by the JVM
Typically implemented using the threads model provided by underlying OS
Java threads may be created by:
• Extending Thread class (and override its run() method)
• Implementing the Runnable interface
(Standard practice is to implement Runnable interface)
Calling start()
• Allocates memory and initializes a new thread in the JVM
• Calls the run() method, making the thread eligible to run on the JVM
Operating System Concepts – 10th Edition 4.26 Silberschatz, Galvin and Gagne ©2018
Java Threads
Implementing Runnable interface:
Creating a thread:
Waiting on a thread:
Operating System Concepts – 10th Edition 4.27 Silberschatz, Galvin and Gagne ©2018
Java Executor Framework
Rather than explicitly creating threads, Java also allows thread creation
around the Executor interface:
The Executor is used as follows:
Operating System Concepts – 10th Edition 4.28 Silberschatz, Galvin and Gagne ©2018
Java Executor Framework
Operating System Concepts – 10th Edition 4.29 Silberschatz, Galvin and Gagne ©2018
Java Executor Framework (Cont.)
Operating System Concepts – 10th Edition 4.30 Silberschatz, Galvin and Gagne ©2018
Implicit Threading
Growing in popularity as numbers of threads increase, program correctness more
difficult with explicit threads
Creation and management of threads done by compilers and run-time libraries
rather than programmers
This strategy, termed implicit threading, is an increasingly popular trend.
Five alternative methods explored
• Thread Pools -- will be briefly discussed.
• Fork-Join
• OpenMP
• Grand Central Dispatch
• Intel Threading Building Blocks
These strategies generally require application developers to identify
tasks—not threads—that can run in parallel. A task is usually written as
a function, which the run-time library then maps to a separate thread,
typically using the many-to-many model
The advantage is that developers only need to identify parallel tasks,
and the libraries determine the specific details of thread creation and
management.
Operating System Concepts – 10th Edition 4.31 Silberschatz, Galvin and Gagne ©2018
Thread Pools
Create a number of threads in a pool where they await work
(+) Advantages:
• Usually slightly faster to service a request with an existing thread
than create a new thread
• Allows the number of threads in the application(s) to be bound to
the size of the pool
(-) Unlimited threads could exhaust system resources, such as CPU
time or memory
Windows API supports thread pools:
Operating System Concepts – 10th Edition 4.32 Silberschatz, Galvin and Gagne ©2018
Java Thread Pools
Three factory methods for creating thread pools in Executors class:
Operating System Concepts – 10th Edition 4.33 Silberschatz, Galvin and Gagne ©2018
Java Thread Pools (Cont.)
Operating System Concepts – 10th Edition 4.34 Silberschatz, Galvin and Gagne ©2018
End of Chapter 4
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018