Download presentation
Presentation is loading. Please wait.
Published byKevin Horton Modified over 9 years ago
1
CHAPTER 5 THREADS & MULTITHREADING 1
2
Single and Multithreaded Processes 2
3
Benefits of Multithreading Responsiveness Multithreading 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 Sharing Threads 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. 3
4
Benefits of Multithreading (Cont’d) Economy Allocating 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. Scalability A single-threaded process can only run on one processor, regardless how many are available. Multithreading on a multi-CPU machine increases parallelism. 4
5
Example: Multithreaded Server Architecture 5
6
Multicore Programming Multithreading imposes some challenges in multicore systems: 1) Dividing activities Examining applications to find areas that can be divided into separate, concurrent tasks 2) Balance Ensure 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 splitting The data accessed and manipulated by the tasks must be divided to run on separate cores. 6
7
Multicore Programming (Cont’d) 1) Data dependency The 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 debugging When a program is running in parallel on multiple cores, there are many different execution paths that need to be tested. 7
8
Concurrent Execution on a Single-core System 8
9
Parallel Execution on a Multicore System 9
10
Threads A thread (or lightweight process) is a basic unit of CPU utilization; it consists of: Program counter Register 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. 10
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) 11
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. 12
13
Threads vs. Processes A thread has no data segment or heapA 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 creationExpensive creation Inexpensive context switchingExpensive context switching If a thread dies, its stack is reclaimedIf a process dies, its resources are reclaimed & all threads die 13
14
Threads Types There are two types of threads: User threads Kernel threads 14
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. 15
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) 16
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. 17
18
In multiprocessor environment, the kernel can schedule threads on different processors. Examples - Windows 95/98/NT - Solaris - Digital UNIX Kernel Threads (cont’d) 18
19
Multithreading Models There are three multithreading models: 1) Many-to-One 2) One-to-One 3) Many-to-Many 19
20
Many-to-One Many 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. 20
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 21
22
One-to-One Model 22
23
Many-to-Many Model Multiplexes many user-level threads to smaller or equal number of kernel threads. 23
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.