Chapter 4: Threads &
Concurrency
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
Overview
Process vs Thread
Thread Creation
Multicore Programming
• Concurrency
• Parallelism ( Data and Task)
Types of threads – User and kernel
Multithreading Models
• One to one
• One to many
• Many to many
Threading Issues
thread cancellation
signal handling
fork () and exec() semantics
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 designing
multithreaded applications
Discuss multithreading models
Highlight threading issues.
Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018
Threads
A thread is a basic unit of CPU utilization; it comprises:
• Thread ID
• Program counter
• Register set
• 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.
If a process has multiple threads of control, it can perform more than
one task at a time.
Operating System Concepts – 10th Edition 4.4 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 such as:
• Update display
• Fetch data
• Spell checking
• Answer a network request
Process creation is heavy-weight while thread creation is light-weight
Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018
Single and Multithreaded Processes
Operating System Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018
Benefits of Thread
Responsiveness – very responsive as compared to processes
Resource Sharing – threads share resources of process, easier
than shared memory or message passing
Economy – cheaper than process creation, thread switching
lower overhead than context switching
Scalability – it can take advantage of multicore architectures
Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018
Process vs Thread
Operating System Concepts – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
Multicore Programming
Multicore or multiprocessor systems puts pressure on programmers,
challenges include:
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging
Concurrency refers to the ability to manage multiple tasks seemingly
happening at once by rapidly switching between them on a single
processor with the help of schedulers.
Parallelism refers to actually executing multiple tasks simultaneously
on multiple processors or cores, achieving true simultaneous execution
rather than just the illusion of it.
Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallelism
Concurrent execution on single-core system:
Parallelism on a multi-core system:
Operating System Concepts – 10th Edition 4.10 Silberschatz, Galvin and Gagne ©2018
Concurrency vs Parallelism
Operating System Concepts – 10th Edition 4.11 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.
• E.g., Summing array of N-elements. E.g., Thread A, [0] . . .
[N/2 − 1], and [N/2] . . . [N − 1].
• Task parallelism – distributing threads across cores, each
thread performing unique operation
• E.g., Performing two different statistical operations on the
same array of N-elements.
Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018
Data and Task Parallelism
Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018
Types of Threads
Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018
User Threads and Kernel Threads
User level threads – ULT management
done by user-level threads library
(POSIX Pthreads, Windows threads,
Java threads)
• Usually fast operations
• Faster context switching
• If a ULT performs I/O operation,
entire process gets blocked.
Kernel level threads – KLT Supported
by the OS (Kernel) – Java threads
Usually slow operations as system calls
are used.
Slow context switching
If one KLT is blocked, no effect on
others.
Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018
Thread Libraries
Thread library provides programmer with API for creating and
managing threads
Two primary ways of implementing
• Library entirely in user space
• Kernel-level library supported by the OS
Operating System Concepts – 10th Edition 4.16 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
Specification, not implementation
API specifies behavior of the thread library, implementation is up to
development of the library
Common in UNIX operating systems (Linux & Mac OS X)
Operating System Concepts – 10th Edition 4.17 Silberschatz, Galvin and Gagne ©2018
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Operating System Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018
Many-to-One
Many user-level threads mapped to
single kernel thread
One thread blocking causes all to
block
Multiple threads may not run in parallel
on multicore system because only one
may be in kernel at a time
Few systems currently use this model
Advantage
The kernel remains unaware to user-
level threads, allowing for lightweight
management and swift context
switching.
Disadvantage
restricted parallelism and the risk of
blocking system calls halting the
entire process
Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018
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
Advantage
• Enhances parallelism
• Allows other threads to continue
executing even when one is blocked
• Ideal for applications requiring high
concurrency
Disadvantage
Overhead for thread creation and context
switching can impact performance.
Operating System Concepts – 10th Edition 4.20 Silberschatz, Galvin and Gagne ©2018
Many-to-Many
Allows many user level threads to be mapped to many kernel threads
Allows the operating system to create a sufficient number of kernel
threads
Windows with the ThreadFiber package
Advantage
-Synthesizes the benefits of both the many-
to-one and one-to-one models
-Improves resource utilization and
parallelism
Disadvantage
-Balances user and kernel threads, and
introduces complexity in implementation
and management.
Operating System Concepts – 10th Edition 4.21 Silberschatz, Galvin and Gagne ©2018
Threading Issues
1. Thread cancellation of the target thread
- Asynchronous or Deferred
2. Signal handling
- Synchronous and asynchronous
3. Semantics of fork() and exec() system calls
Operating System Concepts – 10th Edition 4.22 Silberschatz, Galvin and Gagne ©2018
Thread Cancellation
The act of terminating a thread before it has finished its execution.
• E.g. database search, to stop web browsing
The thread to be cancelled is target thread ( victim)
Two general approaches:
• Asynchronous cancellation terminates the target thread
immediately
• Difficulty:
If communication is half way, it will disturb that.
Resource allocation is to be reclaimed by OS
• Deferred cancellation allows the target thread to periodically
check if it should be cancelled or not.
This approach will maintain stability in system.
On Linux systems, thread cancellation is handled through signals
Operating System Concepts – 10th Edition 4.23 Silberschatz, Galvin and Gagne ©2018
Termination vs Cancellation
Feature Thread termination Thread cancellation
Who stops the thread? The thread itself Another thread
requests cancellation
Controlled exit? Yes, normal Depends (forceful or
termination follows graceful)
cleanup steps
Risky? No, resources are Can cause resource
properly cleaned up leaks (when a program
fails to properly release
a system resource) if
forceful
Operating System Concepts – 10th Edition 4.24 Silberschatz, Galvin and Gagne ©2018
Signal Handling
Signals are used in UNIX systems to notify a process that a particular
event has occurred.
Synchronous- Internal / Direct generation of signals on specific event
occurrence, directly sent to a process that operated to cause signal.
• E.g. Traps , illegal memory access
Asynchronous – External signals generation , may be sent to another
process.
• E,g. Interrupts
Both of these follow the same pattern for handling.
Signals can be :
• Ignored
• Blocked
• Handled
Operating System Concepts – 10th Edition 4.25 Silberschatz, Galvin and Gagne ©2018
Signal Handler
A signal handler( function) is used to process signals
1. Signal is generated by a particular event
2. Signal is delivered to a process
3. Signal is handled by one of two signal handlers:
1. Default
2. user-defined
Every signal has default handler that kernel runs when handling
signal
• User-defined signal handler can override default handler
• For single-threaded, signal delivered to process
Operating System Concepts – 10th Edition 4.26 Silberschatz, Galvin and Gagne ©2018
Signal Handling (Cont.)
Signal handling is easy in single-threaded process while in
multithreaded environment handling can be complex.
Where should a signal be delivered for multi-threaded?
Following options are available
• Deliver the signal to the thread to which the signal applies
• Deliver the signal to every thread in the process
• Deliver the signal to certain threads in the process
• Assign a specific thread to receive all signals for the process
Operating System Concepts – 10th Edition 4.27 Silberschatz, Galvin and Gagne ©2018
Semantics of fork() and exec()
Does fork()duplicate only the calling thread or all threads?
• Some UNIXes have two versions of fork
exec() usually works as normal – replace the running process
including all threads
Operating System Concepts – 10th Edition 4.28 Silberschatz, Galvin and Gagne ©2018
End of Chapter 4
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018