Threads - Running Parallel Processes & Enhancing Performance

 
Threads
 
prepared and instructed by
 Shmuel Wimer
Eng. Faculty, Bar-Ilan University
 
1
 
November 2016
 
Threads
2
November 2016
Threads
 
Threads
 are kind of processes within a process, sharing
address space, running as they were separate processes.
Useful on systems with multiple CPUs, where real
parallelism exists, also in single CPU with multiple ALUs.
In many applications multiple activities are going on at
once. Some may block from time to time.
Decomposing it into multiple sequential threads running
quasi-parallel simplifies programming model.
Threads creation and destruction is X10-X100 faster than
processes (context switch).
Thread Usage
November 2016
Threads
3
 
A thread switch should be much more efficient than a
process switch, which typically requires hundreds to
thousands of processor cycles.
Thread Hardware
 
Overview
 
4
 
November 2016
 
Threads
 
Having multiple threads running in parallel in one
process is analogous to having multiple processes
running in parallel in one computer.
 
5
 
November 2016
 
Threads
 
Single-threaded and multithreaded processes.
November 2016
Threads
6
November 2016
Threads
7
 
Example
: A web server accepts concurrently thousands
client requests for web pages, images, sound, etc.
Single-threaded process is able to service only one client
at a time. Client will wait long time for service.
Upon request the server creates a separate, time and
resource consuming process. A huge overhead.
November 2016
Threads
8
 
Most today’s 
OS kernels 
are multithreaded. Each thread
performs a specific task, such as managing devices,
managing memory, or interrupt handling.
The benefits of multithreaded programming are:
Responsiveness to user
. For instance, when user clicks
button triggering execution of time-consuming task.
Resource sharing
. Processes share resources by shared
memory and message passing, handled by programmer.
Threads share automatically memory and resources of
the process to which they belong.
November 2016
Threads
9
 
Economy
. Allocating memory and resources for process
creation is costly.
X30 slower process creation than thread, X5 slower
context switching (
Solaris
).
Scalability. 
Benefits are greater in multiprocessor, where
threads run in parallel on different cores.
Single-threaded process can run on only one processor,
regardless how many are available.
 
Like process, a thread can be in 
running
, 
blocked
, 
ready
,
or 
terminated
 states. state transitions are same as for
process.
Running
 thread currently has the CPU and is active.
Blocked
 thread is waiting for some event to unblock it,
e.g. read from the keyboard.
Ready
 thread is scheduled to run and will when its turn
comes up.
Each thread has its own stack, containing one 
frame
 for
each procedure called but not yet returned. It contains
the procedure’s local variables and the return address.
10
November 2016
Threads
 
November 2016
 
Threads
 
11
 
Multiple CPUs, named 
multicore 
or
 multiprocessor
(MP) 
systems, are integrated on single chip.
 
Multicore Programming
 
Parallel execution on a multicore system.
November 2016
Threads
12
 
A core appears as a separate processor to the OS.
Multithreaded programming 
provides more efficiency
and improved concurrency.
A system is 
parallel
 if it performs more than one task
simultaneously.
In contrast, a 
concurrent
 system supports more than
one task by allowing all the tasks to make progress.
Modern Intel CPUs support 2 threads per core (4-16),
Oracle T5 CPU supports 16 threads per core (16).
November 2016
Threads
13
 
Identifying tasks
. Divide application into separate,
concurrent tasks, running in parallel on individual cores.
Balance
. Define tasks performing equal work of equal
value.
Data splitting
. The data accessed and manipulated by
the tasks should be divided to run on separate cores.
Data dependency
. When task depends on data of
another, ensure that tasks execution is in sync.
Testing and debugging
. It is more difficult for concurrent
programs than single-threaded applications.
MP Programming Challenges
November 2016
Threads
14
Types of Parallelism
 
November 2016
 
Threads
 
15
 
User threads 
are managed without kernel support.
Kernel threads 
are supported and managed directly by
the OS.
Relationship exists between user and kernel threads.
 
Multithreading Models
November 2016
Threads
16
 
The 
many-to-one
 model maps many user-level threads
to one kernel thread.
Thread management is done by the library in user space,
so it is efficient.
The entire process will block if a thread makes a blocking
system call.
Because only one thread can access the kernel at a time,
multiple threads are 
unable
 to run in 
parallel
 on
multicore systems.
One-to-one
 model maps each user thread to a kernel
thread.
November 2016
Threads
17
 
One-to-one provides more concurrency than many-to-
one by allowing another thread to run when thread
makes blocking system call.
It also allows multiple threads to run in parallel on
multiprocessors.
Linux and MS-Windows OS implement one-to-one.
 
November 2016
 
Threads
 
18
 
A drawback is that creating a user thread requires
creating the corresponding kernel thread.
Because overhead of creating kernel threads burden
performance, one-to-one implementations restrict the
number of threads in the system.
Many-to-one
 allows creation of many user threads, but
not true concurrency, because of the single thread.
One-to-one
 allows greater concurrency, but care must
be taken not to create too many threads.
November 2016
Threads
19
 
A 
thread library 
provides the programmer with an API
for creating and managing threads.
There are two ways of implementing a thread library.
User space 
with no kernel support. All code and data
structures for the library exist in user space.
Invoking a function in the library results in a local
function call in user space and not a system call.
The second approach implements a 
kernel-leve
l library
supported directly by the OS.
Thread Libraries
November 2016
Threads
20
November 2016
Threads
21
Each thread runs independently of other threads. The
parent need not know when its child terminates.
Because of independency, there is little data sharing
between threads.
 
Synchronous
 threading occurs when the parent must
wait for all of its children to terminate before it resumes,
called 
fork-join
 
strategy.
 
November 2016
 
Threads
 
22
 
November 2016
 
Threads
 
23
 
November 2016
 
Threads
 
24
 
POSIX Pthread code for joining ten threads.
Slide Note
Embed
Share

Threads are essential for achieving parallelism in processes, boosting performance, and improving responsiveness in modern computer applications. Learn about the benefits and applications of multithreaded programming.

  • Threads
  • Parallelism
  • Multithreading
  • Performance
  • Applications

Uploaded on Mar 03, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University November 2016 Threads 1

  2. Thread Usage Threads are kind of processes within a process, sharing address space, running as they were separate processes. Useful on systems with multiple CPUs, where real parallelism exists, also in single CPU with multiple ALUs. In many applications multiple activities are going on at once. Some may block from time to time. Decomposing it into multiple sequential threads running quasi-parallel simplifies programming model. Threads creation and destruction is X10-X100 faster than processes (context switch). November 2016 Threads 2

  3. Thread Hardware A thread switch should be much more efficient than a process switch, which typically requires hundreds to thousands of processor cycles. November 2016 Threads 3

  4. Overview Having multiple threads running in parallel in one process is analogous to having multiple processes running in parallel in one computer. November 2016 Threads 4

  5. Single-threaded and multithreaded processes. November 2016 Threads 5

  6. Most applications running on modern computers are multithreaded. An application is implemented as a separate process with several threads. Example: Web browser has a thread displaying images or text. Another thread retrieves data from network. Example: Word processor has graphics display thread, another responding keystrokes, and thread performing background spelling and grammar checking. CPU-intensive applications perform tasks in parallel to leverage processing capabilities on multicore systems. November 2016 Threads 6

  7. Example: A web server accepts concurrently thousands client requests for web pages, images, sound, etc. Single-threaded process is able to service only one client at a time. Client will wait long time for service. Upon request the server creates a separate, time and resource consuming process. A huge overhead. Multithreaded server architecture. November 2016 Threads 7

  8. Most todaysOS kernels are multithreaded. Each thread performs a specific task, such as managing devices, managing memory, or interrupt handling. The benefits of multithreaded programming are: Responsiveness to user. For instance, when user clicks button triggering execution of time-consuming task. Resource sharing. Processes share resources by shared memory and message passing, handled by programmer. Threads share automatically memory and resources of the process to which they belong. November 2016 Threads 8

  9. Economy. Allocating memory and resources for process creation is costly. X30 slower process creation than thread, X5 slower context switching (Solaris). Scalability. Benefits are greater in multiprocessor, where threads run in parallel on different cores. Single-threaded process can run on only one processor, regardless how many are available. November 2016 Threads 9

  10. Like process, a thread can be in running, blocked, ready, or terminated states. state transitions are same as for process. Running thread currently has the CPU and is active. Blocked thread is waiting for some event to unblock it, e.g. read from the keyboard. Ready thread is scheduled to run and will when its turn comes up. Each thread has its own stack, containing one frame for each procedure called but not yet returned. It contains the procedure s local variables and the return address. November 2016 Threads 10

  11. Multicore Programming Multiple CPUs, named multicore or multiprocessor (MP) systems, are integrated on single chip. time Parallel execution on a multicore system. November 2016 Threads 11

  12. A core appears as a separate processor to the OS. Multithreaded programming provides more efficiency and improved concurrency. A system is parallel if it performs more than one task simultaneously. In contrast, a concurrent system supports more than one task by allowing all the tasks to make progress. Modern Intel CPUs support 2 threads per core (4-16), Oracle T5 CPU supports 16 threads per core (16). November 2016 Threads 12

  13. MP Programming Challenges Identifying tasks. Divide application into separate, concurrent tasks, running in parallel on individual cores. Balance. Define tasks performing equal work of equal value. Data splitting. The data accessed and manipulated by the tasks should be divided to run on separate cores. Data dependency. When task depends on data of another, ensure that tasks execution is in sync. Testing and debugging. It is more difficult for concurrent programs than single-threaded applications. November 2016 Threads 13

  14. Types of Parallelism Data parallelism.Distributes subsets of same data across multiple cores, each performing the same operation. Example: Summing an N-size array. On single-core, one thread sums the elements [0] . . . [N 1]. On dual-core the data can be split into two halves for two threads running in parallel on separate cores. Task parallelism.Distributes tasks (threads) across multiple cores, Each performing a unique operation. Different threads may operate on the same or different data. Applications may use a hybrid of parallelism types. November 2016 Threads 14

  15. Multithreading Models User threads are managed without kernel support. Kernel threads are supported and managed directly by the OS. Relationship exists between user and kernel threads. Many-to-one model. November 2016 Threads 15

  16. The many-to-one model maps many user-level threads to one kernel thread. Thread management is done by the library in user space, so it is efficient. The entire process will block if a thread makes a blocking system call. Because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multicore systems. One-to-one model maps each user thread to a kernel thread. November 2016 Threads 16

  17. One-to-one model. One-to-one provides more concurrency than many-to- one by allowing another thread to run when thread makes blocking system call. It also allows multiple threads to run in parallel on multiprocessors. Linux and MS-Windows OS implement one-to-one. November 2016 Threads 17

  18. A drawback is that creating a user thread requires creating the corresponding kernel thread. Because overhead of creating kernel threads burden performance, one-to-one implementations restrict the number of threads in the system. Many-to-one allows creation of many user threads, but not true concurrency, because of the single thread. One-to-one allows greater concurrency, but care must be taken not to create too many threads. November 2016 Threads 18

  19. Thread Libraries A thread library provides the programmer with an API for creating and managing threads. There are two ways of implementing a thread library. User space with no kernel support. All code and data structures for the library exist in user space. Invoking a function in the library results in a local function call in user space and not a system call. The second approach implements a kernel-level library supported directly by the OS. November 2016 Threads 19

  20. Library code and data structures exist in kernel space. Invoking a function in the API for the library results in a system call to the kernel. Example: A multithreaded program that performs ??? = ?=0 ?. The upper bounds of the summation is entered on the command line. ? There are two strategies to create multiple threads: asynchronous and synchronous. With asynchronous, once the parent thread creates a child thread, the parent resumes its execution, so parent and child execute concurrently. November 2016 Threads 20

  21. Each thread runs independently of other threads. The parent need not know when its child terminates. Because of independency, there is little data sharing between threads. Synchronous threading occurs when the parent must wait for all of its children to terminate before it resumes, called fork-joinstrategy. November 2016 Threads 21

  22. Thread header Parent thread fork join November 2016 Threads 22

  23. November 2016 Threads 23

  24. POSIX Pthread code for joining ten threads. November 2016 Threads 24

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#