Threads
Threads
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 4: Threads
Overview
Multicore Programming
Multithreading Models
Thread Libraries
Implicit Threading
Threading Issues
Operating System Concepts – 9th Edition 4.2 Silberschatz, Galvin and Gagne ©2013
Motivation
Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©2013
What is?
A thread is a basic unit of CPU utilization; a fundamental unit of CPU utilization that forms the basis
of multithreaded computer systems
It comprises
a thread ID,
a program counter,
a register set,
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.
Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Single and Multithreaded Processes
Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Benefits
Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©2013
4.2 Multicore Programming
Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
4.2 Multicore Programming
Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
Concurrent execution on single-core system:
Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
Multicore Programming (Cont.)
Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
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 – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Multicore Programming (Cont.)
Types of parallelism
Data parallelism – distributes subsets of the same data
across multiple cores, same operation on each
For example, summing the contents of an array of size N.
On a single-core system, one thread would simply sum
the elements [0] . . . [N − 1].
On a dual-core system, however, thread A, running on
core 0, could sum the elements [0] . . . [N/2 − 1] while
thread B, running on core 1, could sum the elements [N/2]
. . . [N − 1].
Task parallelism – distributing threads across cores, each
thread performing unique operation
For example, two threads, each performing a unique
statistical operation on the array of elements. The threads
again are operating in parallel on separate computing
cores, but each is performing a unique operation.
Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
4.3 Multithreading Models
User Threads and Kernel Threads
User threads - management done by user-level threads library
Three primary thread libraries:
POSIX Pthreads
Windows threads
Java threads
Kernel threads - Supported by the Kernel and managed by the OS
Examples – virtually all general purpose operating systems, including:
Windows
Solaris
Linux
Tru64 UNIX
Mac OS X
Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
4.3 Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Many-to-One
Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
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
It also allows multiple threads to run in parallel
on multiprocessors
Number of threads per process sometimes
restricted due to overhead
Examples
Windows
Linux
Solaris 9 and later
Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model
Allows many user level threads to be
mapped to many kernel threads
When a thread performs a blocking
system call, the kernel can schedule
another thread for execution
Solaris prior to version 9
Windows with the ThreadFiber
package
Operating System Concepts – 9th Edition 4.17 Silberschatz, Galvin and Gagne ©2013
Thread Libraries
For POSIX and Windows threading, any data declared globally—that is,
declared outside of any function—are shared among all threads
belonging to the same process.
Java has no notion of global data, access to shared data must be
explicitly arranged between threads
Data declared local to a function are typically stored on the stack
Since each thread has its own stack, each thread has its own copy of
local data.
Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
Asynchronous vs synchronous threading
Asynchronous threading
Once the parent creates a child thread, the parent resumes its execution, so that the parent and
child execute concurrently
Each thread runs independently of every other thread
E.g. Multithreaded web server
Synchronous threading
Parent thread creates one or more children and then must wait for all of its children to terminate
before it resumes
Known as fork-join strategy
Threads can run concurrently but parent cannot continue until this work has been
completed
Synchronous threading involves significant data sharing among threads
Operating System Concepts – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
Pthreads
Operating System Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013
Pthreads Example
Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Pthreads Example (Cont.)
Operating System Concepts – 9th Edition 4.22 Silberschatz, Galvin and Gagne ©2013
Pthreads Code for Joining 10 Threads
Operating System Concepts – 9th Edition 4.23 Silberschatz, Galvin and Gagne ©2013
Windows Multithreaded C Program
Operating System Concepts – 9th Edition 4.24 Silberschatz, Galvin and Gagne ©2013
Windows Multithreaded C Program (Cont.)
Operating System Concepts – 9th Edition 4.25 Silberschatz, Galvin and Gagne ©2013
Java Threads
Operating System Concepts – 9th Edition 4.26 Silberschatz, Galvin and Gagne ©2013
Java Multithreaded Program
Operating System Concepts – 9th Edition 4.27 Silberschatz, Galvin and Gagne ©2013
Java Multithreaded Program (Cont.)
Creating a Thread object
does not specifically create the
new thread; rather,
the start() method creates the
new thread
Operating System Concepts – 9th Edition 4.28 Silberschatz, Galvin and Gagne ©2013
Implicit Threading
Operating System Concepts – 9th Edition 4.29 Silberschatz, Galvin and Gagne ©2013
OpenMP
Set of compiler directives and an
API for C, C++, FORTRAN
Provides support for parallel
programming in shared-memory
environments
Identifies parallel regions –
blocks of code that can run in
parallel
#pragma omp parallel
Create as many threads as there are
cores
#pragma omp parallel for
for(i=0;i<N;i++) {
c[i] = a[i] + b[i];
}
Run for loop in parallel
Operating System Concepts – 9th Edition 4.30 Silberschatz, Galvin and Gagne ©2013
Threading Issues
Operating System Concepts – 9th Edition 4.31 Silberschatz, Galvin and Gagne ©2013
Thread Cancellation
Terminating a thread before it has finished
Thread to be canceled is target thread
Two general approaches:
Asynchronous cancellation terminates the target thread
immediately
Deferred cancellation allows the target thread to periodically
check if it should be cancelled
Pthread code to create and cancel a thread:
Operating System Concepts – 9th Edition 4.32 Silberschatz, Galvin and Gagne ©2013
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
The functions pthread_key_create and pthread_key_delete are used
respectively to create and delete a key for thread-specific data.
The type of the key is explicitly left opaque and is referred to as
pthread_key_t.
Operating System Concepts – 9th Edition 4.33 Silberschatz, Galvin and Gagne ©2013
News in C11
Multi-threading support (_Thread_local storage-class specifier, <threads.h> header including thread
creation/management functions, mutex, condition variable and thread-specific storage functionality
Operating System Concepts – 9th Edition 4.34 Silberschatz, Galvin and Gagne ©2013
News in C++11
#include <threads.h>
thread_local int foo = 0;
C++11 introduces the thread_local keyword
Operating System Concepts – 9th Edition 4.35 Silberschatz, Galvin and Gagne ©2013
End of Chapter 4
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013