Sie sind auf Seite 1von 29

Chapter : Threads

• Overview
• Benefits of Threads vs Processes
• Single and Multithreaded Processes
• Types of Threads
• Multithreading Models
• Thread Libraries
Threads
• It is active entity.
– Executes unit of process
• Works simultaneously with others.
– Many threads executing together.
• Requires coordination.
– Sharing of resources, CPU and memory.
• Threads may execute same program but different
sections of programs.
Threads
• It is single sequential (flow of) execution of tasks of process
• A thread (or lightweight process) is a basic unit of CPU
utilization; it consists of:
– program counter
– register set
– stack space
• A thread shares with its peer threads its:
– code section
– data section
– operating-system resources

• Most modern applications are multithreaded.


Threads (Cont.)
• Threads run within application .
• Improves application performance through parallelism.
• A traditional or heavyweight process has a single thread of
control. Only one task at time.

• In a multiple threaded task, while one thread is blocked and


waiting, a second thread in the same task can run.

– In web browser, one thread displays images, other text, other


fetches data from network.

– Word processor, one thread for graphics, second thread for


response to keystrokes and third for spell check.
Single and Multithreaded Processes
Benefits of multithreaded
programming
• Responsiveness – may allow continued execution if
part of process is blocked, especially important for
user interfaces
• Resource Sharing – threads share resources of
process easier than shared memory or message
passing
• Economy – cheaper than process creation, thread
switching lower overhead than context switching
• Scalability – Multithreading on a multi- CPU
machine increases parallelism.
PROCESS VS THREAD
Process Thread
An executing instance of a program
A thread is a subset of the process.
is called a process.
Multiple processes uses more Multiple threads uses fewer
resources resources.
Processes must use inter-process
Threads can directly communicate
communication to communicate
with other threads of its process.
with sibling processes.
Processes have considerable
Threads have almost no overhead.
overhead.
New processes require duplication
New threads are easily created.
of the parent process.
Process switching needs interaction Thread switching does not need to
with operating system. interact with operating system.
TYPES OF THREADS
• Threads can be implemented in two ways:

– User level threads (ULT)


– Kernel level threads (KLT)
User level vs. Kernel level
USER LEVEL THREAD KERNEL LEVEL THREAD
User thread are implemented by kernel threads are implemented by
users. OS.
OS doesn’t recognized user level
Kernel threads are recognized by OS.
threads.
Implementation of User threads is Implementation of Kernel thread is
easy. complicated.
Context switch time is less. Context switch time is more.
Context switch requires no hardware
Hardware support is needed.
support.
If one user level thread perform If one kernel thread perform blocking
blocking operation then entire process operation then another thread can
will be blocked. continue execution.
Example : Java thread, POSIX
Example : Window Solaris.
threads.
Can run on any operating system It is specific to the operating system.
Multithreading Models
• Threads may be provided either at the user level, for
user threads or by the kernel for kernel threads.
• User threads are supported above the kernel and are
managed without kernel support whereas kernel
threads are supported and managed directly by the
operating system.
• Operating systems—including Windows, Linux, Mac
OS X, Solaris, and Tru64 UNIX support kernel
threads.
Multithreading Models
• One-to-One

• Many-to-One

• Many-to-Many
One-to-one Model
ONE TO ONE MODEL
• Maps each user thread to a kernel thread.
• All threads can access the kernel at the same
time.
• Linux, along with the family of Windows
operating systems implement the one-to-one
model.
ONE TO ONE MODEL
Advantages:
• OS sees / understands threads, synchronization,
scheduling or blocking.

Disadvantages:
• Must go to kernel/OS for all operations.
• May be expensive.
• OS may limits on policies, number of threads that can be
executed on CPU.
• Portability.
Many-to-One
• It associates all user- level
threads to a single kernel-
level thread.
• All threads activity is
restricted in user space as it
is done in thread library
that decides which thread
will be scheduled on kernel
thread.
• Examples:
– Solaris Green Threads
– GNU Portable
Threads
Many-to-One
Advantages:
• Totally portable.
• It is efficient.
• Does not depend on OS limits and policies because all
scheduling, synchronization etc are done at user level.
Disadvantages:
• OS has no insight into application needs.
• OS may block entire process if one user level thread
blocks on I/O.
• Multiple threads are unable to run in parallel on
multiprocessors
Many-to-Many Model
• Also known as two level model.
• Multiplexes many user-level threads to an equal or
smaller number of kernel-level threads.
• Allows the operating system to create a sufficient
number of kernel threads
• Also allows a user-level thread to be bound to a kernel
thread.
• Supported by operating systems such as
– IRIX, HP-UX, and Tru64 UNIX, The Solaris
Many-to-Many Model
MANY TO MANY MODEL
Advantages:
• It is best of both.
• Can have bound or unbound threads.
Disadvantages:
• Requires coordination between user level and kernel
level threads.
Thread Cancellation
• Thread cancellation is the task of terminating a
thread before it has completed.
– For example, if multiple threads are
concurrently searching through a database
and one thread returns the result, the
remaining threads might be canceled.
– Another situation might occur when a user
presses a button on a web browser that stops
a web page from loading any further.
1) The model in which one kernel thread is
mapped to many user-level threads is called:
a) Many to One model
b) One to Many model
c) Many to Many model
d) One to One model
• Which of the following is the drawback of the
One to One Model ?
a) increased concurrency provided by this
model
b) decreased concurrency provided by this
model
c) creating so many threads at once can crash
the system
d) creating a user thread requires creating the
corresponding kernel thread
• Which one of the following is not shared by
threads?
a) program counter
b) stack
c) both (a) and (b)
d) none of the mentioned
• If one thread opens a file with read privileges
then
a) other threads in the another process can
also read from that file
b) other threads in the same process can also
read from that file
c) any other thread can not read from that file
d) all of the mentioned
• Termination of the process terminates
a) first thread of the process
b) first two threads of the process
c) all threads within the process
d) no thread within the process
• A thread shares its resources(like data section,
code section, open files, signals) with :
a) other process similar to the one that the
thread belongs to
b) other threads that belong to similar
processes
c) other threads that belong to the same
process
d) All of these
• A heavy weight process :
a) has multiple threads of execution
b) has a single thread of execution
c) can have multiple or a single thread for
execution
d) None of these
• A thread is also called :
a) Light Weight Process(LWP)
b) Heavy Weight Process(HWP)
c) Process
d) None of these
• Which one of the following is not a valid state
of a thread?
a) running
b) parsing
c) ready
d) blocked

Das könnte Ihnen auch gefallen