Threads

188 views 11:18 am 0 Comments June 27, 2023

Operating System Concepts
Chapter 4 { Threads
Based on the 9th Edition of:
Abraham Silberschatz, Peter B. Galvin and Jreg Gagne:.
Operating System
Concepts
Department of Information Technology, College of Business, Law & Governance
Motivation Multicore Multithread Libraries Thread Issues Examples
Learning 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
Chapter 4 { Threads Operating System Concepts 2
Motivation Multicore Multithread Libraries Thread Issues Examples
Outline
1 Motivation
2 Multicore Programming
3 Multithreading Modes
4 Thread Libraries
5 Threading Issues
6 Operating System Examples
Chapter 4 { Threads Operating System Concepts 3
Motivation Multicore Multithread Libraries Thread Issues Examples
Motivation
Most modern applications are multithreaded
Threads run within application
Multiple tasks with the application can be implemented by
separate threads (e.g., 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
Chapter 4 { Threads Operating System Concepts 4
Motivation Multicore Multithread Libraries Thread Issues Examples
Motivation
Single-threaded and multithreaded processes
registers
code data files
stack registers registers registers
code data files
stack stack stack
thread thread
single-threaded process multithreaded process
Chapter 4 { Threads Operating System Concepts 5
Motivation Multicore Multithread Libraries Thread Issues Examples
Motivation
Multithreaded server architecture
client
(1) request
(2) create new
thread to service
the request
(3) resume listening
for additional
client requests
server thread
Chapter 4 { Threads Operating System Concepts 6
Motivation Multicore Multithread Libraries Thread Issues Examples
Motivation
Benefits
Responsiveness { may allow continued execution if part of
process is blocked, especially important for user interfaces
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 { process can take advantage of multiprocessor
architectures
Chapter 4 { Threads Operating System Concepts 7
Motivation Multicore Multithread Libraries Thread Issues Examples
Motivation
Multicore Programming
Multicore or multiprocessor systems putting pressure on
programmers, challenges include:
Dividing activities
Balance
Data splitting
Data dependency
Testing and debugging
Parallelism implies a system can perform more than one task
simultaneously
Concurrency supports more than one task making progress
Single processor/core, scheduler providing concurrency
Chapter 4 { Threads Operating System Concepts 8
Motivation Multicore Multithread Libraries Thread Issues Examples
Motivation
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
Chapter 4 { Threads Operating System Concepts 9
Motivation Multicore Multithread Libraries Thread Issues Examples
Motivation
Concurrent execution on a single-core system
single core T1 T2 T3 T4 T1 T2 T3 T4 T1
time

Parallel execution on a multicore system
core 1 T1 T3 T1 T3 T1
core 2 T2 T4 T2 T4 T2
time
… …
Chapter 4 { Threads Operating System Concepts 10
Motivation Multicore Multithread Libraries Thread Issues Examples
Multithreading Modes
User Threads and Kernel Threads
User threads { management done by user-level threads library
Three primary thread libraries: POSIX Pthreads, Windows
threads, and Java threads
Kernel threads { Supported by the Kernel
Examples { virtually all general purpose operating systems,
including: Windows, Solaris, Linux, Tru64 UNIX, and Mac OS
X
Chapter 4 { Threads Operating System Concepts 11
Motivation Multicore Multithread Libraries Thread Issues Examples
Multithreading Modes
Relationship Between User Threads and Kernel Threads
Many-to-One
One-to-One
Many-to-Many
Chapter 4 { Threads Operating System Concepts 12
Motivation Multicore Multithread Libraries Thread Issues Examples
Multithreading Modes
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
Examples: Solaris and GNU
user thread
k kernel thread
Chapter 4 { Threads Operating System Concepts 13
Motivation Multicore Multithread Libraries Thread Issues Examples
Multithreading Modes
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
Examples: Windows, Linux, and Solaris 9 and later
user thread
k k k k kernel thread
Chapter 4 { Threads Operating System Concepts 14
Motivation Multicore Multithread Libraries Thread Issues Examples
Multithreading Modes
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
Example: Solaris prior to
version 9
user thread
k k k kernel thread
Chapter 4 { Threads Operating System Concepts 15
Motivation Multicore Multithread Libraries Thread Issues Examples
Multithreading Modes
Two-level Model
Similar to
many-to-many,
except that it allows
a user thread to be
bound to kernel
thread
Example: IRIX,
HP-UX, Tru64 UNIX,
and Solaris 8 and
earlier
user thread
k k k k kernel thread
Chapter 4 { Threads Operating System Concepts 16
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The multithreading model multiplexes many user-level
threads to a smaller or equal number of kernel threads.
A. many-to-one model
B. one-to-one model
C. many-to-many model
D. many-to-some model
Answer:
Chapter 4 { Threads Operating System Concepts 17
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The multithreading model multiplexes many user-level
threads to a smaller or equal number of kernel threads.
A. many-to-one model
B. one-to-one model
C. many-to-many model
D. many-to-some model
Answer: C
Chapter 4 { Threads Operating System Concepts 17
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The multithreading model multiplexes many user-level
threads to a smaller or equal number of kernel threads.
A. many-to-one model
B. one-to-one model
C. many-to-many model
D. many-to-some model
Answer: C
2 is not considered a challenge when designing
applications for multicore systems.
A. Deciding which activities can be run in parallel
B. Ensuring there is a sufficient number of cores
C. Determining if data can be separated so that it is accessed on
separate cores
D. Identifying data dependencies between tasks.
Answer:
Chapter 4 { Threads Operating System Concepts 17
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The multithreading model multiplexes many user-level
threads to a smaller or equal number of kernel threads.
A. many-to-one model
B. one-to-one model
C. many-to-many model
D. many-to-some model
Answer: C
2 is not considered a challenge when designing
applications for multicore systems.
A. Deciding which activities can be run in parallel
B. Ensuring there is a sufficient number of cores
C. Determining if data can be separated so that it is accessed on
separate cores
D. Identifying data dependencies between tasks.
Answer: B
Chapter 4 { Threads Operating System Concepts 17
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The model maps many user-level threads to one kernel
thread.
A. many-to-many
B. two-level
C. one-to-one
D. many-to-one
Answer:
Chapter 4 { Threads Operating System Concepts 18
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The model maps many user-level threads to one kernel
thread.
A. many-to-many
B. two-level
C. one-to-one
D. many-to-one
Answer: D
Chapter 4 { Threads Operating System Concepts 18
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The model maps many user-level threads to one kernel
thread.
A. many-to-many
B. two-level
C. one-to-one
D. many-to-one
Answer: D
2 True or False { A traditional (or heavyweight) process has a
single thread of control.
Answer:
Chapter 4 { Threads Operating System Concepts 18
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The model maps many user-level threads to one kernel
thread.
A. many-to-many
B. two-level
C. one-to-one
D. many-to-one
Answer: D
2 True or False { A traditional (or heavyweight) process has a
single thread of control.
Answer: True
Chapter 4 { Threads Operating System Concepts 18
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The model maps many user-level threads to one kernel
thread.
A. many-to-many
B. two-level
C. one-to-one
D. many-to-one
Answer: D
2 True or False { A traditional (or heavyweight) process has a
single thread of control.
Answer: True
3 True or False { Virtually all contemporary operating
systems support kernel threads.
Answer:
Chapter 4 { Threads Operating System Concepts 18
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 The model maps many user-level threads to one kernel
thread.
A. many-to-many
B. two-level
C. one-to-one
D. many-to-one
Answer: D
2 True or False { A traditional (or heavyweight) process has a
single thread of control.
Answer: True
3 True or False { Virtually all contemporary operating
systems support kernel threads.
Answer: True
Chapter 4 { Threads Operating System Concepts 18
Motivation Multicore Multithread Libraries Thread Issues Examples
Thread Libraries
Thread library provides programmer with API for creating and
managing threads
Two primary ways of implementing
1 Library entirely in user space
2 Kernel-level library supported by the OS
Pthreads refers to the POSIX standard (IEEE 1003.1c)
defining an API for thread creation and synchronization
{common in UNIX operating systems (Solaris, Linux, and Mac
OS X)
Java threads are managed by the JVM, and threads may be
created by:
public interface Runnable
f
public abstract void run(); g
Chapter 4 { Threads Operating System Concepts 19
Motivation Multicore Multithread Libraries Thread Issues Examples
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
Separating task to be performed from mechanics of creating
task allows different strategies for running task, i.e., tasks
could be scheduled to run periodically
Windows API supports thread pools:
Chapter 4 { Threads Operating System Concepts 20
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Pthreads refers to .
A. the POSIX standard.
B. an implementation for thread behavior.
C. a specification for thread behavior.
D. an API for process creation and synchronization.
Answer:
Chapter 4 { Threads Operating System Concepts 21
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Pthreads refers to .
A. the POSIX standard.
B. an implementation for thread behavior.
C. a specification for thread behavior.
D. an API for process creation and synchronization.
Answer: A
Chapter 4 { Threads Operating System Concepts 21
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Pthreads refers to .
A. the POSIX standard.
B. an implementation for thread behavior.
C. a specification for thread behavior.
D. an API for process creation and synchronization.
Answer: A
2 involves distributing tasks across multiple computing
cores.
A. Concurrency
B. Task parallelism
C. Data parallelism
D. Parallelism
Answer:
Chapter 4 { Threads Operating System Concepts 21
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Pthreads refers to .
A. the POSIX standard.
B. an implementation for thread behavior.
C. a specification for thread behavior.
D. an API for process creation and synchronization.
Answer: A
2 involves distributing tasks across multiple computing
cores.
A. Concurrency
B. Task parallelism
C. Data parallelism
D. Parallelism
Answer: B
Chapter 4 { Threads Operating System Concepts 21
Motivation Multicore Multithread Libraries Thread Issues Examples
Threading Issues
Semantics of fork() and exec() system calls
Signal handling (synchronous and asynchronous)
Thread cancellation of target thread (asynchronous or
deferred)
Thread-local storage
Scheduler Activations
Chapter 4 { Threads Operating System Concepts 22
Motivation Multicore Multithread Libraries Thread Issues Examples
Threading Issues
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
Chapter 4 { Threads Operating System Concepts 23
Motivation Multicore Multithread Libraries Thread Issues Examples
Threading Issues
Signal Handling
Signals are used in UNIX systems to notify a process that a
particular event has occurred. Signal is generated by
particular event and delivered to a process.
A
signal handler is used to process signals
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
For single-threaded, signal delivered to process
Chapter 4 { Threads Operating System Concepts 24
Motivation Multicore Multithread Libraries Thread Issues Examples
Threading Issues
Signal Handling (Cont.)
Where should a signal be delivered for multi-threaded?
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
Chapter 4 { Threads Operating System Concepts 25
Motivation Multicore Multithread Libraries Thread Issues Examples
Threading Issues
Thread Cancellation
Terminating a thread before it has finished
Thread to be canceled is
target thread
Two general approaches:
1 Asynchronous cancellation terminates the target thread
immediately
2 Deferred cancellation allows the target thread to periodically
check if it should be canceled
Chapter 4 { Threads Operating System Concepts 26
Motivation Multicore Multithread Libraries Thread Issues Examples
Threading Issues
Thread-Local Storage
Thread-local storage (TLS) allows each thread to have its
own copy of data
Useful when you do not have control over the thread creation
process (i.e., when using a thread pool)
Different from local variables
Local variables visible only during single function invocation
TLS visible across function invocations
Similar to static data {TLS is unique to each thread
Chapter 4 { Threads Operating System Concepts 27
Motivation Multicore Multithread Libraries Thread Issues Examples
Threading Issues
Scheduler Activation
Both Many-to-Many and Two-level models require
communication to maintain the appropriate number of kernel
threads allocated to the application
Typically use an intermediate data structure between user and
kernel threads {
lightweight process (LWP)
Appears to be a virtual processor on which process can
schedule user thread to run
Each LWP attached to kernel thread
Chapter 4 { Threads Operating System Concepts 28
Motivation Multicore Multithread Libraries Thread Issues Examples
Threading Issues
Scheduler Activation (Cont.)
Scheduler activations
provide
upcalls {a
communication mechanism
from the kernel to the
upcall
handler
in the thread library
This communication allows
an application to maintain
the correct number kernel
threads
LWP
user thread
k kernel thread
lightweight process
Chapter 4 { Threads Operating System Concepts 29
Motivation Multicore Multithread Libraries Thread Issues Examples
Operating System Examples
Windows Threads
Windows implements the Windows API { primary API for Win
98, Win NT, Win 2000, Win XP, and Win 7
Implements the one-to-one mapping, kernel-level
Each thread contains
A thread id
Register set representing state of processor
Separate user and kernel stacks for when thread runs in user
mode or kernel mode
Private data storage area used by run-time libraries and
dynamic link libraries (DLLs)
The register set, stacks, and private storage area are known as
the
context of the thread
Chapter 4 { Threads Operating System Concepts 30
Motivation Multicore Multithread Libraries Thread Issues Examples
Operating System Examples
Windows Threads (Cont.)
The primary data structures of a thread include:
ETHREAD (executive thread block) { includes pointer to
process to which thread belongs and to KTHREAD, in kernel
space
KTHREAD (kernel thread block) { scheduling and
synchronization info, kernel-mode stack, pointer to TEB, in
kernel space
TEB (thread environment block) { thread id, user-mode stack,
thread-local storage, in user space
Chapter 4 { Threads Operating System Concepts 31
Motivation Multicore Multithread Libraries Thread Issues Examples
Operating System Examples
Data structures of a Windows XP thread
kernel space user space
pointer to
parent process
thread start
address
ETHREAD
KTHREAD



kernel
stack
scheduling
and
synchronization
information



user
stack
thread-local
storage
thread identifier
TEB



Chapter 4 { Threads Operating System Concepts 32
Motivation Multicore Multithread Libraries Thread Issues Examples
Operating System Examples
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
flag meaning
CLONE_FS
CLONE_VM
CLONE_SIGHAND
CLONE_FILES
File-system information is shared.
The same memory space is shared.
Signal handlers are shared.
The set of open files is shared.
Chapter 4 { Threads Operating System Concepts 33
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Cancellation points are associated with cancellation.
A. asynchronous
B. deferred
C. synchronous
D. non-deferred
Answer:
Chapter 4 { Threads Operating System Concepts 34
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Cancellation points are associated with cancellation.
A. asynchronous
B. deferred
C. synchronous
D. non-deferred
Answer: B
Chapter 4 { Threads Operating System Concepts 34
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Cancellation points are associated with cancellation.
A. asynchronous
B. deferred
C. synchronous
D. non-deferred
Answer: B
2 Which of the following would be an acceptable signal handling
scheme for a multithreaded program?
A. Deliver the signal to the thread to which the signal applies.
B. Deliver the signal to every thread in the process.
C. Deliver the signal to only certain threads in the process.
D. All of the above
Answer:
Chapter 4 { Threads Operating System Concepts 34
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Cancellation points are associated with cancellation.
A. asynchronous
B. deferred
C. synchronous
D. non-deferred
Answer: B
2 Which of the following would be an acceptable signal handling
scheme for a multithreaded program?
A. Deliver the signal to the thread to which the signal applies.
B. Deliver the signal to every thread in the process.
C. Deliver the signal to only certain threads in the process.
D. All of the above
Answer: D
Chapter 4 { Threads Operating System Concepts 34
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Thread-local storage is data that .
A. is not associated with any process
B. has been modified by the thread, but not yet updated to the
parent process
C. is generated by the thread independent of the thread’s process
D. is unique to each thread
Answer:
Chapter 4 { Threads Operating System Concepts 35
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Thread-local storage is data that .
A. is not associated with any process
B. has been modified by the thread, but not yet updated to the
parent process
C. is generated by the thread independent of the thread’s process
D. is unique to each thread
Answer: D
Chapter 4 { Threads Operating System Concepts 35
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Thread-local storage is data that .
A. is not associated with any process
B. has been modified by the thread, but not yet updated to the
parent process
C. is generated by the thread independent of the thread’s process
D. is unique to each thread
Answer: D
2 True or False { Linux distinguishes between processes and
threads.
Answer:
Chapter 4 { Threads Operating System Concepts 35
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Thread-local storage is data that .
A. is not associated with any process
B. has been modified by the thread, but not yet updated to the
parent process
C. is generated by the thread independent of the thread’s process
D. is unique to each thread
Answer: D
2 True or False { Linux distinguishes between processes and
threads.
Answer: False
Chapter 4 { Threads Operating System Concepts 35
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Thread-local storage is data that .
A. is not associated with any process
B. has been modified by the thread, but not yet updated to the
parent process
C. is generated by the thread independent of the thread’s process
D. is unique to each thread
Answer: D
2 True or False { Linux distinguishes between processes and
threads.
Answer: False
3 True or False { Deferred cancellation is preferred over
asynchronous cancellation.
Answer:
Chapter 4 { Threads Operating System Concepts 35
Motivation Multicore Multithread Libraries Thread Issues Examples
Quick Quiz
1 Thread-local storage is data that .
A. is not associated with any process
B. has been modified by the thread, but not yet updated to the
parent process
C. is generated by the thread independent of the thread’s process
D. is unique to each thread
Answer: D
2 True or False { Linux distinguishes between processes and
threads.
Answer: False
3 True or False { Deferred cancellation is preferred over
asynchronous cancellation.
Answer: True
Chapter 4 { Threads Operating System Concepts 35
Motivation Multicore Multithread Libraries Thread Issues Examples
End of Chapter 4
Chapter 4 { Threads Operating System Concepts 36

Tags: , , , , , , , , , , ,