4. 4.4
ObjectivesObjectives
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
5. 4.5
MotivationMotivation
Most modern applications are multithreaded
Threads run within application
Multiple tasks with the application can be implemented by separate
threads
Update display / mouse on screen
Fetch data
Spell checking
Answer a network request
Process creation is heavy-weight while thread creation is light-
weight (less resources)
Can simplify code, increase efficiency
Kernels are generally multithreaded
6. 4.6
Chapter 4: ThreadsChapter 4: Threads
A thread is a basic unit of CPU utilization (use) ,(Lightweight
process: LWP), not stand alone process but a part of it (process).
Each thread has its own ID , stack, program counter, and registers.
Threads share common code, data, and open files.
Each single window under word process is a single thread.
Figure x. Lightweight process (LWP).
9. 4.9
BenefitsBenefits
Responsiveness: Faster execution.
Resource Sharing: By default threads share common code,
data, and other resources, which allows multiple tasks to be
performed simultaneously in a single address space.
threads share resources of process, easier than shared
memory or message passing
Economy :Creating and managing threads ( and context switches
between them ) is much faster than performing the same tasks for
processes.
cheaper than process creation, thread switching lower
overhead than context switching
Utilization of MP Architectures: A single threaded process
can only run on one CPU, whereas the execution of a multi-
threaded application may be split between available processors.
Scalability – process can take advantage of multiprocessor
architectures
10. 4.10
Multicore ProgrammingMulticore 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
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
12. 4.12
Types of threadsTypes of threads
User threads
Threads that created and managed in the user space , without
kernel support.
User threads - management done by user-level threads library
These are the threads that application programmers put into
their programs.
user-direct to- CPU (execution)
Kernel acts with processes not with threads.
Kernel threads
Kernel threads - Supported by the Kernel
threads are supported (managed, created, killed) by the OS.
Kernel acts with threads.
Examples – virtually all general purpose operating systems,
including: Windows, Solaris, Linux, Tru64 UNIX and Mac OS X.
14. 4.14
Many-to-OneMany-to-One
Many user-level threads mapped to single kernel thread
One thread blocking causes all to block
If a blocking system call is made, then the entire process blocks.
Multiple threads may not run in parallel on Multicore system because only one
may be in kernel at a time
Single kernel thread can operate only on a single CPU, so the many-to-one
model does not allow individual processes to be split across multiple CPUs.
Few systems currently use this model
Examples:
Solaris Green Threads
GNU Portable Threads
Figure x. Many-to-one model.
15. 4.15
One-to-OneOne-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
The overhead of managing the one-to-one model is more
significant, involving more overhead and slowing down the
system.
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
Figure x. One-to-one model.
16. 4.16
Many-to-Many ModelMany-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
Block a specific thread ,unlike many-one
Less overhead, unlike one-one
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber
package
Figure x. Many-to-many model.
17. 4.17
Two-level ModelTwo-level Model
Similar to M:M, except that it allows a user thread to be bound to
kernel thread
A popular variation of the many-to-many model is the two-tier model,
which allows either many-to-many or one-to-one operation.
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
Figure x. Two-level model.
18. 4.18
Thread LibrariesThread Libraries
Thread library provides programmer with API for creating and
managing threads
Two primary ways of implementing
Library entirely in user space
Kernel-level library supported by the OS
19. 4.19
PthreadsPthreads
May be provided either as user-level or kernel-level
A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
POSIX stands for Portable Operating System Interface for Unix
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)
20. 4.20
Windows ThreadsWindows Threads
The technique for creating threads using the Windows thread
library is similar to the Pthreads technique in several ways.
21. 4.21
Java ThreadsJava Threads
Threads are the fundamental model of program execution in a Java
program, and the Java language and its API provide a rich set of features for
the creation and management of threads.
Java threads are managed by the JVM
Typically implemented using the threads model provided by underlying OS
Java threads may be created by:
Extending Thread class
Implementing the Runnable interface
22. 4.22
Implicit ThreadingImplicit 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 –
Create a number of threads in a pool where they await work
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.
Grand Central Dispatch –
Apple technology for Mac OS X and iOS operating systems
Extensions to C, C++ languages, API, and run-time library
Allows identification of parallel sections
Other methods include Microsoft Threading Building Blocks (TBB),
java.util.concurrent package
23. 4.23
Thread PoolsThread Pools
Create a number of threads in a pool where they await work.
Advantages: (Thread pools offer these benefits:)
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.
A sample thread pool (green boxes) with waiting tasks (blue) and completed tasks
(yellow)
25. 4.25
Semantics of fork() and exec()Semantics of fork() and exec()
Does fork() duplicate only the calling thread or all threads?
o Some UNIXes have two versions of fork
There are two options:
Create a child process that contains identical copy of the whole
threads in the parent process.
OR
Create a child process that contains a copy of the thread that
executed the fork call only.
Exec() usually works as normal – replace the running process
including all threads
26. 4.26
Thread CancellationThread 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 (synchronous) cancellation allows the target
thread to periodically check if it should be cancelled.
Why :
Data consistent , …… updating.
On Linux systems, thread cancellation is handled through signals
Signals are used in UNIX systems to notify a process that a
particular event has occurred.
A signal handler is used to process signals. (See next slide)
27. 4.27
Signal HandlingSignal Handling
Signals are used in UNIX systems to notify a process that a particular event has
occurred
A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled
Signal is handled by one of two signal handlers:
1. Default signal handler
2. User-defined signal handler
Options:
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
28. 4.28
Thread-Local StorageThread-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.
29. 4.29
Scheduler ActivationsScheduler Activations
Both M:M 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.
How many LWPs to create?
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
Figure x. Lightweight process (LWP).
30. 4.30
Operating System ExamplesOperating System Examples
Windows XP 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
The register set, stacks, and private storage area are known as
the context of the thread
Linux Thread:
Linux refers to them as tasks rather than threads.
Thread creation is done through clone() system call.