Chapter 4: Threads & Concurrency: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts - 10 Edition
Chapter 4: Threads & Concurrency: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts - 10 Edition
Concurrency
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Objectives
To introduce the notion of a thread
To examine issues related to multithreaded programming
To cover operating system support for threads
Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018
Concurrency and Parallelism
Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018
Concurrency and Parallelism
Parallelism implies that a system can perform more than one task
simultaneously
Operating System Concepts – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallelism
Concurrent execution on single-core system:
Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018
Types of Parallelism
Data parallelism – distributes subsets of the same data across multiple
cores, same operation on each
Operating System Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018
Amdahl’s Law
Identifies performance gains from adding additional cores to an
application that has both serial and parallel components
S is serial portion
N processing cores
Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018
Introduction to Threads and
Multithreading
Operating System Concepts – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
What is a thread?
Traditional (single-threaded) process has a single thread of control.
The thread shares with other threads (belonging to the same process) its
code section, data section, and other OS resources, such as open files.
A process with multiple threads can do more than one task at a time.
What resources are used when a thread is created? How do they differ
from those used when a process is created?
Operating System Concepts – 10th Edition 4.10 Silberschatz, Galvin and Gagne ©2018
Examples of MT Applications
Web browser
Word processor
Web-server
Problem?
Two possible solutions
Suggest another application that would benefit from the use of threads,
and an application that would not.
Operating System Concepts – 10th Edition 4.11 Silberschatz, Galvin and Gagne ©2018
Multithreaded Server Architecture
Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018
Benefits of MT Applications
Responsiveness – may allow continued execution if part of
process is blocked, especially important for user interfaces
Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018
ST Example
def calculate_sum_single_thread(N):
total = 0
for i in range(1, N + 1):
total += i
return total
N = 10000
result = calculate_sum_single_thread(N)
print({result})
Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018
MT Example
import threading
N = 10000
thread1_result = [ ]
thread2_result = [ ]
Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018
MT Example (Cont.)
# Split the work into two threads
half_N = N / 2
thread1 = threading.Thread(target=calculate_sum_partial, args=(N,
1, half_N, thread1_result))
thread2 = threading.Thread(target=calculate_sum_partial, args=(N,
half_N + 1, N, thread2_result))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
Operating System Concepts – 10th Edition 4.17 Silberschatz, Galvin and Gagne ©2018
User Threads
Implemented by a thread library at the user level (above the kernel)
The library provides support for thread creation, scheduling, and
management.
The programmer of the library writes code to synchronize threads and to
context switch them, and they all run in one process.
Kernel does not provide any support; it is unaware that user-level
threads are even running.
Fast to create and manage (i.e., high efficiency), mainly because the
kernel is not involved.
What if the kernel is single-threaded?
Three primary thread libraries: POSIX Pthreads, Windows threads, Java
threads
Operating System Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018
Kernel Threads
Supported directly by the OS
The kernel performs thread creation, scheduling, and management
in kernel space.
Slower to create and manage than user threads and have more
overhead in the kernel.
If a thread performs a blocking system call, the kernel can schedule
another thread in the application for execution.
In a parallel environment, the kernel can schedule threads on different
processors.
Examples – virtually all general-purpose OS’s, including Windows,
Solaris, Linux, Mac OS X
Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018
Multithreading Models
Operating System Concepts – 10th Edition 4.20 Silberschatz, Galvin and Gagne ©2018
MT Models
Many-to-One
One-to-One
Many-to-Many
Operating System Concepts – 10th Edition 4.21 Silberschatz, Galvin and Gagne ©2018
Many-to-One MT
Many user-level threads mapped to
single kernel thread
One thread blocking causes all to block
Multiple threads may not run in parallel
on muti-core system because only one
may be in kernel at a time
Used in systems that do not support
kernel threads
Few systems currently use this model
Examples:
Solaris Green Threads
GNU Portable Threads
Operating System Concepts – 10th Edition 4.22 Silberschatz, Galvin and Gagne ©2018
One-to-One MT
Each user-level thread maps to a kernel thread
Examples:
Windows
Linux
Solaris 9 and later
Operating System Concepts – 10th Edition 4.23 Silberschatz, Galvin and Gagne ©2018
Many-to-Many MT
Allows many user-level threads to be
mapped to many kernel threads
Examples:
Solaris prior to version 9
Windows with the ThreadFiber
package
Operating System Concepts – 10th Edition 4.24 Silberschatz, Galvin and Gagne ©2018
Two-Level MT
Similar to the many-to-many model, except that it allows a
user thread to be bound to kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
Operating System Concepts – 10th Edition 4.25 Silberschatz, Galvin and Gagne ©2018
MT Models: Summary
Many-to-one
The developer can create as many user threads as he wishes
But the true concurrency is not gained because the kernel can
schedule only one thread at a time
One-to-one
Greater concurrency than many-to-one
But the developer has to be careful (and in some instances be
limited) not to create too many threads within an application
Many-to-many
Does not suffer from the above shortcomings
Developers can create as many user threads as necessary, and the
corresponding kernel threads can run in parallel on a multiprocessor
Operating System Concepts – 10th Edition 4.26 Silberschatz, Galvin and Gagne ©2018
End of Chapter 4
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018