Lec04 2-Threads
Lec04 2-Threads
Concurrency
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Chapter 4: Threads
Overview
Multicore Programming
Multithreading Models
Thread Libraries
Implicit Threading
Threading Issues
Operating System Examples
Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018
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
Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018
Motivation
Operating System Concepts – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018
Multithreaded Server Architecture
Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018
Benefits
Operating System Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018
Multicore Programming
Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018
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 – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallelism
Concurrent execution on single-core system:
Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018
Single and Multithreaded Processes
Operating System Concepts – 10th Edition 4.10 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.12 Silberschatz, Galvin and Gagne ©2018
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018
Many-to-One
Operating System Concepts – 10th Edition 4.14 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
Solaris 9 and later
Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018
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 – 10th Edition 4.16 Silberschatz, Galvin and Gagne ©2018
Two-level Model
Similar to M:M, 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.17 Silberschatz, Galvin and Gagne ©2018
Thread Libraries
Operating System Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018
Pthreads
Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018
Pthreads Example
Operating System Concepts – 10th Edition 4.20 Silberschatz, Galvin and Gagne ©2018
Pthreads
Pthreads Example
Example (Cont.)(Cont.)
Operating System Concepts – 10th Edition 4.21 Silberschatz, Galvin and Gagne ©2018
Pthreads Code for Joining 10 Threads
Operating System Concepts – 10th Edition 4.22 Silberschatz, Galvin and Gagne ©2018
Windows Multithreaded C Program
Operating System Concepts – 10th Edition 4.23 Silberschatz, Galvin and Gagne ©2018
Windows Multithreaded C Program (Cont.)
Operating System Concepts – 10th Edition 4.24 Silberschatz, Galvin and Gagne ©2018
Java Threads
Operating System Concepts – 10th Edition 4.27 Silberschatz, Galvin and Gagne ©2018
Java Multithreaded Program (Cont.)
Operating System Concepts – 10th Edition 4.28 Silberschatz, Galvin and Gagne ©2018
Implicit Threading
Operating System Concepts – 10th Edition 4.30 Silberschatz, Galvin and Gagne ©2018
Fork Join
The main parent thread creates (forks) one or more child threads
and then waits for the children to terminate and join with it
This thread creation technique is an excellent candidate for
implicit threading
A library manages the number of threads that are created and is
also responsible for assigning tasks to threads.
In some ways, this fork-join model is a synchronous version of
thread pools in which a library determines the actual number of
threads to create
Operating System Concepts – 10th Edition 4.31 Silberschatz, Galvin and Gagne ©2018
Fork Join in Java
Java introduced a fork-join library in Version 1.7 of the API that is
designed to be used with recursive divide-and-conquer
algorithms such as Quicksort and Mergesort.
Separate tasks are forked during the divide step and assigned
smaller subsets of the original problem
The general recursive algorithm behind Java's fork-join model:
Task(problem)
if problem is small enough
solve the problem directly
else
subtask1 = fork(new Task(subset of problem)
subtask2 = fork(new Task(subset of problem)
result1 = join(subtask1)
result2 = join(subtask2)
Operating System Concepts – 10th Edition 4.32 Silberschatz, Galvin and Gagne ©2018
Fork Join in Java
Example: a divide-and-conquer algorithm that sums all elements
in an array of integers.
In Version 1.7 of the API Java introduced a new thread pool—the
ForkJoinPool—that can be assigned tasks that inherit the abstract
base class ForkJoinTask (which for now we will assume is the
SumTask class).
The following creates a ForkJoinPool object and submits the initial
task via its invoke() method:
ForkJoinPool pool = new ForkJoinPool();
// array contains the integers to be summed
int[] array = new int[SIZE];
Operating System Concepts – 10th Edition 4.33 Silberschatz, Galvin and Gagne ©2018
Fork Join in Java
import java.util.concurrent.*;
public class SumTask extends RecursiveTask<Integer>
{
static final int THRESHOLD = 1000;
return sum;
}
else {
int mid = (begin + end) / 2;
leftTask.fork();
rightTask.fork();
Operating System Concepts – 10th Edition 4.35 Silberschatz, Galvin and Gagne ©2018