Upload
kevin-horton
View
228
Download
3
Embed Size (px)
Citation preview
1
CHAPTER 5THREADS & MULTITHREADING
2
Single and Multithreaded Processes
3
Benefits of Multithreading
ResponsivenessMultithreading an interactive application may allow a program to
continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user.
Resource SharingThreads share the memory and the resources of the process to
which they belong by default. The benefit of sharing code and data is that it allows an
application to have several different threads of activity within the same address space.
4
Benefits of Multithreading (Cont’d)
EconomyAllocating memory and resources for process creation is costly. Because threads share the resources of the process to which they
belong, it is more economical to create and context-switch threads.
ScalabilityA single-threaded process can only run on one processor,
regardless how many are available. Multithreading on a multi-CPU machine increases parallelism.
5
Example: Multithreaded Server Architecture
6
Multicore Programming
Multithreading imposes some challenges in multicore systems:
1) Dividing activitiesExamining applications to find areas that can be divided into separate,
concurrent tasks
2) BalanceEnsure that the tasks perform equal work of equal value. In some instances, a certain task may not contribute as much value to
the overall process as other tasks.
3) Data splittingThe data accessed and manipulated by the tasks must be divided to
run on separate cores.
7
Multicore Programming (Cont’d)
1) Data dependencyThe data accessed by the tasks must be examined for
dependencies between two or more tasks. In instances where one task depends on data from another,
programmers must ensure that the execution of the tasks is synchronized to accommodate the data dependency.
2) Testing and debuggingWhen a program is running in parallel on multiple cores, there are
many different execution paths that need to be tested.
8
Concurrent Execution on a Single-core System
9
Parallel Execution on a Multicore System
10
Threads
A thread (or lightweight process) is a basic unit of CPU
utilization; it consists of:Program counterRegister set Stack space
Threads are lightweight. Why?There is no thread-specific heap or data segment (unlike
process).Therefore, context switching is much cheaper than for a process.
11
A thread shares with its peer threads its:Code section.Data section.Operating system resources.
A traditional or heavyweight process is equal to a task with one thread.
Threads (cont’d)
12
Why use threads?
Multiple CPUs. "scientific" code, like matrix multiply.Modularity. one program, many semi-independent tasks.
don't want them to know about each other's control flow.Example? background spell checker in Microsoft word.makes sense on both uni- and multi-processors.
Concurrency. keep disk, CPU, net busy; avoid blocking.Makes sense on both uni- and multi-processors.
13
Threads vs. Processes
A thread has no data segment or heap A process has code/data/heap & other segments
A thread cannot live on its own, it must live within a process
There must be at least one thread in a process
There can be more than one thread in a process, the first thread calls main & has the process’s stack
Threads within a process share code/data/heap, share I/O, but each has its own stack & registers
Inexpensive creation Expensive creation
Inexpensive context switching Expensive context switching
If a thread dies, its stack is reclaimed If a process dies, its resources are reclaimed & all threads die
14
Threads TypesThere are two types of threads:
User threadsKernel threads
15
User Threads
Thread Management Done by User-Level Threads Library.
The library provides support for thread creation, scheduling, and management with support from the kernel.
All thread creation and scheduling are done in user space without the need for kernel intervention, Kernel is unaware of user-level threads.
User-level threads are generally fast to create and manage.
16
If the kernel is single threaded, then any user-level thread performing a blocking system call will cause the entire process to block
Examples
- POSIX Pthreads
- Mach C-threads
- Solaris threads
User Threads (cont’d)
17
Kernel Threads
Supported by the Kernel : Thread creating,
scheduling, and management are done by the kernel
in kernel space.Kernel threads are generally slower to create and
manage than are user threads.If a thread performs a blocking system call, the kernel
can schedule another thread in the application for execution.
18
In multiprocessor environment, the kernel can schedule threads on different processors.
Examples- Windows 95/98/NT
- Solaris- Digital UNIX
Kernel Threads (cont’d)
19
Multithreading ModelsThere are three multithreading models:
1) Many-to-One
2) One-to-One
3) Many-to-Many
20
Many-to-OneMany User-Level Threads Mapped to Single Kernel Thread.Multiple threads are unable to run in parallel on
multiprocessors.Used on Systems That Do Not Support Kernel Threads.
21
One-to-One
Each User-Level Thread Maps to Kernel Thread.Allows another thread to run when a thread makes a
blocking system call (concurrency).It allows multiple threads to run in parallel on
multiprocessors.Creating a user thread requires creating the
corresponding kernel thread.
Examples:
- Windows 95/98/NT
- OS/2
22
One-to-One Model
23
Many-to-Many Model Multiplexes many user-level threads to smaller or equal
number of kernel threads.