CH 3 Process and Thread Part 2
CH 3 Process and Thread Part 2
Threads
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
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 cover operating system support for threads in Windows and
Linux
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
Benefits
Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Multicore Programming
Multicore or multiprocessor systems putting pressure
on programmers, challenges include:
1. Identifying tasks - Examining applications to find activities that can be
performed concurrently.
2. Balance - Finding tasks to run concurrently that provide equal value. I.e.,
don't waste a thread on trivial tasks.
3. Data splitting - To prevent the threads from interfering with one another.
4. Data dependency - If one task is dependent upon the results of another,
then the tasks need to be synchronized to assure access in the proper
order.
5. Testing and debugging - Inherently more difficult in parallel processing
situations, as the race conditions become much more complex and
difficult to identify.
Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
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
Operating System Concepts – 9th Edition 4.6 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
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 – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Single and Multithreaded Processes
Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Multithreading Models
There are two types of threads to be managed in a
modern system:
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
Examples – virtually all general purpose operating systems, including:
Windows, Solaris, Linux, Tru64 UNIX, and Mac OS X
Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
Multithreading Models
Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
Many-to-One
Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
One-to-One
Each user-level thread maps to a kernel thread
Creating a user-level thread creates a kernel thread
More concurrency than many-to-one
The only drawback to this model is that :
creating a user thread requires creating the corresponding kernel
thread. Because the overhead of creating kernel threads can burden
the performance of an application, most implementations of this model
restrict the number of threads supported by the system.
Examples
Windows
Linux
Solaris 9 and later
Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model
Allows many user level threads to be mapped to a smaller or equal
number of kernel threads.
The number of kernel threads may be specific to either a particular
application or a particular machine
Allows the operating system to create a sufficient number of kernel
threads
Examples
Solaris prior to version 9
Windows with the ThreadFiber package
Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
Design Effect on Concurrency
The many-to-one model allows the developer to create as many user
threads as she/he wishes, it does not result in true concurrency, because
the kernel can schedule only one thread at a time.
The one-to-one model allows greater concurrency, but the developer has to
be careful not to create too many threads within an application to limit
overhead.
Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Design Effect on Concurrency
The many-to-many model does not suffer from any of
these shortcomings:
developers can create as many user threads as necessary, and
the corresponding kernel threads can run in parallel on a
multiprocessor.
Also, when a thread performs a blocking system call, the kernel
can schedule another thread for execution.
Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
Design Effect on Concurrency
One variation on the many-to-many model still multiplexes
many user level threads to a smaller or equal number of
kernel threads but also allows a user-level thread to be
bounded to a kernel thread. This variation is sometimes
referred to as the two-level model.
Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
Two-level Model
Similar to M:M, except that it allows a user thread to be
bounded to kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
Operating System Concepts – 9th Edition 4.17 Silberschatz, Galvin and Gagne ©2013
Thread Libraries
Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
POSIX Pthread Libraries
Institute of Electrical and Electronics Engineers (IEEE).
A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization.
Pthreads, the threads extension of the POSIX standard,
may be provided as either a user-level or a kernel-
level library.
Specification, not implementation
API specifies behavior of the thread library, implementation is up
to development of the library
Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Operating System Concepts – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
Windows threads Libraries
Operating System Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013
Java Threads
Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Java Threads
Operating System Concepts – 9th Edition 4.22 Silberschatz, Galvin and Gagne ©2013
Implicit Threading
Growing in popularity as numbers of threads increase,
program correctness more difficult with explicit threads
Creation and management of threads done by compilers
and run-time libraries rather than programmers
Three methods explored
Thread Pools
OpenMP
Grand Central Dispatch
Other methods include Microsoft Threading Building
Blocks (TBB), java.util.concurrent package
Operating System Concepts – 9th Edition 4.23 Silberschatz, Galvin and Gagne ©2013
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:
Operating System Concepts – 9th Edition 4.24 Silberschatz, Galvin and Gagne ©2013
OpenMP
OpenMP is a set of compiler directives as
well as an API for programs written in C,
C++, or FORTRAN that 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.25 Silberschatz, Galvin and Gagne ©2013
Grand Central Dispatch
Operating System Concepts – 9th Edition 4.26 Silberschatz, Galvin and Gagne ©2013
Operating System Examples
Windows Threads
Linux Threads
Operating System Concepts – 9th Edition 4.27 Silberschatz, Galvin and Gagne ©2013
Windows Threads
Operating System Concepts – 9th Edition 4.28 Silberschatz, Galvin and Gagne ©2013
Windows Threads (Cont.)
Operating System Concepts – 9th Edition 4.29 Silberschatz, Galvin and Gagne ©2013
Windows Threads Data Structures
Operating System Concepts – 9th Edition 4.30 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 4.31 Silberschatz, Galvin and Gagne ©2013
على حضوركم.. شكرا لكم
وحسن تعاونكم
Operating System Concepts – 9th Edition 4.32 Silberschatz, Galvin and Gagne ©2013
End of Chapter 3
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013