chapter 5 threads & multithreading 1. single and multithreaded processes 2

23
CHAPTER 5 THREADS & MULTITHREADING 1

Upload: kevin-horton

Post on 16-Dec-2015

232 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

1

CHAPTER 5THREADS & MULTITHREADING

Page 2: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

2

Single and Multithreaded Processes

Page 3: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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.

Page 4: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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.

Page 5: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

5

Example: Multithreaded Server Architecture

Page 6: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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.

Page 7: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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.

Page 8: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

8

Concurrent Execution on a Single-core System

Page 9: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

9

Parallel Execution on a Multicore System

Page 10: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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.

Page 11: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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)

Page 12: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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.

Page 13: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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

Page 14: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

14

Threads TypesThere are two types of threads:

User threadsKernel threads

Page 15: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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.

Page 16: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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)

Page 17: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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.

Page 18: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

18

In multiprocessor environment, the kernel can schedule threads on different processors.

Examples- Windows 95/98/NT

- Solaris- Digital UNIX

Kernel Threads (cont’d)

Page 19: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

19

Multithreading ModelsThere are three multithreading models:

1) Many-to-One

2) One-to-One

3) Many-to-Many

Page 20: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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.

Page 21: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

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

Page 22: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

22

One-to-One Model

Page 23: CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2

23

Many-to-Many Model Multiplexes many user-level threads to smaller or equal

number of kernel threads.