Module_3_-_Fork-join_parallelism
Module_3_-_Fork-join_parallelism
‘’
In fork–join parallelism, control flow forks (divides)
into multiple flows that join (combine) later.
After the fork, one flow turns into two separate
flows.
Each flow is independent, and they are not
constrained to do similar computation.
After the join, only one flow continues.
For example, consider forking to
execute B() and C() in parallel and then joining
afterwards.
The execution of a fork–join can be pictured as
a directed graph.
1
‘’
⊡ Often fork–join is used to
implement
recursive divide-and-
conquer algorithms.
⊡ The typical pattern looks
like this:
2
⊡ shows three-level, two-
way nesting, resulting in
‘’
eight parallel flows at the
innermost level
⊡ For good speedup, it is
important that most of the
work occur deep in the
nesting, where parallelism
is high.
3
Multithreading
‘’
⊡ is the ability of a program or an operating system to enable
more than one user at a time without requiring multiple
copies of the program running on the computer.
⊡ can also handle multiple requests from the same user.
⊡ Each user request for a program or system service is
tracked as a thread with a separate identity.
⊡ initial thread request are interrupted by other requests
⊡ the work status of the initial request is tracked until the work
is completed
4
Multithreading
‘’
⊡ Fast central processing unit (CPU) speed and large
memory capacities are needed for multithreading.
⊡ The single processor executes pieces, or threads, of
various programs so fast, it appears the computer is
handling multiple requests simultaneously.
5
Thread ‘’
is an entity within a process that can be scheduled for execution.
it is the smallest unit of processing that can be performed in an OS
(Operating System).
is a sequence of such instructions within a program that can be
executed independently of other code.
simply a subset of a process.
A thread contains all the information in a Thread Control Block
(TCB).
6e
‘’
A thread contains the following information in a Thread
Control Block (TCB):
Thread Identifier: Unique id (TID) is assigned to every new thread
Stack pointer: Points to thread’s stack in the process. Stack
contains the local variables under thread’s scope.
Program counter: a register which stores the address of the
instruction currently being executed by thread.
Thread state: can be running, ready, waiting, start or done.
Thread’s register set: registers assigned to thread for computations.
Parent process Pointer: A pointer to the Process control block
(PCB) of the process that the thread lives on.
7e
‘’
Relationship between the Process and its Thread
8e
How does multithreading work? ‘’
The extremely fast processing speeds of today’s
microprocessors make multithreading possible.
Even though the processor executes only one instruction at a time,
threads from multiple programs are executed so fast that it appears
multiple programs are executing concurrently.
Each CPU cycle executes a single thread that links to all other
threads in its stream.
Synchronization process occurs so quickly that it appears all the
streams are executing at the same time.
9e
How does multithreading work? ‘’
Each thread contains information about how it relates to the overall
program.
Multithreading requires programmers to pay careful attention to
prevent race conditions and deadlock.
10e
An example of multithreading ‘’
When working on a spreadsheet, a user enters data into a cell, and
the following may happen:
column widths may be adjusted;
repeating cell elements may be replicated;
and spreadsheets may be saved multiple times as the file is further
developed.
Each activity occurs because multiple threads are generated and
processed for each activity without slowing down the overall
spreadsheet application operation.
11e
‘’
The main important application areas of multithreading are:
12e
‘’
Multithreading vs. Multiprocessing
13e
‘’
Multithreading vs. Multiprocessing
14e
‘’
Multithreading vs. Multiprocessing
Multithreading is useful for IO-bound processes, such as
reading files from a network or database since each thread can
run the IO-bound process concurrently.
15e
‘’
Multithreading vs. Multiprocessing
Note that using multithreading for CPU-bound processes might slow
down performance due to competing resources that ensure only one
thread can execute at a time, and overhead is incurred in dealing with
multiple threads.
On the other hand, multiprocessing can be used for IO-bound
processes. However, overhead for managing multiple processes is
higher than managing multiple threads as illustrated above. You may
notice that multiprocessing might lead to higher CPU utilization due to
multiple CPU cores being used by the program, which is expected.
16e
Multithreading as a Python Function ‘’
Multithreading can be implemented using the Python built-in
library threading and is done in the following order:
18e
‘’
Multithreading as a Python Function
19e
‘’
Multithreading as a Python Function
Output:
20e
‘’
Multithreading as a Python Class
21e
‘’
Creating a Thread using Thread class
22e
‘’
Creating a Thread with pre-defined ‘Thread’ class in python
23e
‘’
Creating a Thread class by inheriting Thread class in python
24e