Professional Documents
Culture Documents
5.1. Overview
If the process has multiple threads of control, it can do more than one task
at a time.
5.1.1 Motivation
Many
software
packages
that
run
on
modern
desktop
PCs
are
multithreaded.
in the
background.
Solution is to have
o the server run as a single process that accepts requests.
o When the server receives a request, it creates a separate process to
service that request.
o Process creation is very heavyweight, as was shown in the previous
chapter.
o If the new process will perform the same tasks as the existing
process creates unnecessary overhead.
Alternativel y
o More efficient for one process that contains multiple threads to
serve the same purpose.
o This approach would multithread the web -server process .
o The server would create a separate thread that would listen for
client requests;
o when a request was made, rather than creating another process, it
would create another thread to service the request.
Threads also play a vital role in remote procedure call (RPC) systems.
5.1.2 Benefits
Responsiveness :
o 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 inc reasing responsiveness to the user.
o Example a multithreaded web browser allow user interaction in
one thread while an image is being loaded in another thread.
Resource sharing:
o By default, threads share the memory and the resources of the
process to which they belong.
o The benefit of code sharing is that it allows an application to have
several different threads of activity within same address space.
Economy:
o Allocating memory and resources for process creation is costl y.
o Alternativel y, because threa ds share resources of the process to
which they belong, it is more economical to create and context
switch threads.
o It can be difficult to gauge empiricall y the difference in overhead
for creating and maintaining a process rather than a thread, but in
general it is much more time consuming to create and manage
processes than threads.
Multicore Programming
Identifying tasks.
o examine applications to find areas that can be divided into separate,
concurrent tasks. tasks are independent of one another and can run
in parallel on individual cores.
Balance.
o While identifying tasks that can run in parallel ensure that the
tasks perform equal work of equal value.
Data splitting .
o Just as applications are divided into separa te tasks, the data
accessed & manipulated by tasks is divided to run on separate cores.
Data dependency.
o The data accessed by the tasks must be examined for dependencies
between two or more tasks. When one task depends on data from
another, programmers mu st ensure that the execution of the tasks is
synchronized to accommodate the data dependency.
Data parallelism
o focuses on distributing subsets of the same data across multiple
computing cores and performing the same operation on each core.
o Consider, for example, summing the contents of an array of size N.
Task parallelism
o involves distributing tasks (threads) across multiple computing
cores.
o Each thread is performing a unique operation.
o Different threads may be operating on the same data, or they may be
operating on different data.
Threads may be provided at either the user level, for user threads, or b y
the kernel, for kernel threads.
User threads
o supported above the kernel and managed without kernel support
o implemented by a thread library at the user level.
Kernel threads
o Supported and managed directl y by the operating system:
o Contemporary OS Windows NT, Windows 2000, Solaris 2, BeOS,
and Tru64 UNIX (formerl y Digital UN1X) -support kernel threads.
onl y one thread can access the kernel at a time, multiple threads are
unable to run in parallel on multiprocessors.
Windows NT, Windows 2000, and OS/2 implement the one-to-one model.