0% found this document useful (0 votes)
103 views19 pages

Threads- Threading Issues

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
103 views19 pages

Threads- Threading Issues

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

Threads- Threading

Issues
 A thread is a single sequential flow of execution of tasks of
a process so it is also known as thread of execution or
thread of control.

 There is a way of thread execution inside the process of


any operating system.

 Each thread has its own program counter, stack, and set of
registers. However, the threads of a single process might
share the same code and data/file. Threads are also
termed lightweight processes as they share
common resources.
Components of Thread
A thread has the following three
components:
1.Program Counter
2.Register Set
3.Stack space
Need of thread

 Since threads use the same data and


code, the operational cost between
threads is low.
 Creating and terminating a thread is
faster compared to creating or
terminating a process.
 Context switching is faster in threads
compared to processes.
Multi threading

 In Multithreading, the idea is to


divide a single process into multiple
threads instead of creating a whole
new process.

 Multithreading is done to achieve


parallelism and to improve the
performance of the applications
Advantages of Multithreading

Resource Sharing: Threads of a single


process share the same resources such
as code, data/file.
Responsiveness: Program responsiveness
enables a program to run even if part of the
program is blocked or executing a lengthy
operation. Thus, increasing the responsiveness
to the user.
Economy: It is more economical to use
threads as they share the resources of a single
Types of thread
User Level Thread:
User-level threads are implemented and
managed by the user and the kernel is not aware
of it.
 User-level threads are implemented using
user-level libraries and the OS does not
recognize these threads.
 User-level thread is faster to create and
manage compared to kernel-level thread.
 Context switching in user-level threads is
faster.
 If one user-level thread performs a blocking
Kernel level Thread:
Kernel level threads are implemented and
managed by the OS.
 Kernel level threads are implemented using
system calls and Kernel level threads
are recognized by the OS.
 Kernel-level threads are slower to create
and manage compared to user-level
threads.
 Context switching in a kernel-level
thread is slower.
 Even if one kernel-level thread performs a
Threading Issues
•System Call
•Thread Cancellation
•Signal Handling
•Thread Pool
•Thread Specific
Data
 The fork() and exec() system calls
 The fork() is used to create a duplicate process. The meaning of the
fork() and exec() system calls change in a multithreaded program.

 If one thread in a program which calls fork(), does the new process
duplicate all threads, or is the new process single-threaded? If we
take, some UNIX systems have chosen to have two versions of
fork(), one that duplicates all threads and another that duplicates
only the thread that invoked the fork() system call.

 If a thread calls the exec() system call, the program specified in the
parameter to exec() will replace the entire process which includes
all threads.
Signal Handling
 Generally, signal is used in UNIX systems to notify
a process that a particular event has occurred.

 A signal received either synchronously or


asynchronously, based on the source of and the
reason for the event being signalled.
 A signal is generated by the occurrence
of a particular event.
 The signal is delivered to a process.
 Once delivered, the signal must be
handled.
Cancellation
 Thread cancellation is the task of terminating a thread
before it has completed.
 For example − If multiple database threads are
concurrently searching through a database and one
thread returns the result the remaining threads might be
cancelled.
 A target thread is a thread that is to be cancelled,
cancellation of target thread may occur in two different
scenarios −
 Asynchronous cancellation − One thread immediately
terminates the target thread.
 Deferred cancellation − The target thread periodically
checks whether it should terminate, allowing it an
opportunity to terminate itself in an ordinary fashion.
Thread polls
Multithreading in a web server, whenever the server
receives a request it creates a separate thread to service
the request.
Some of the problems that arise in creating a thread are
as follows −
 The amount of time required to create the thread prior
to serving the request together with the fact that this
thread will be discarded once it has completed its work.
 If all concurrent requests are allowed to be serviced in
a new thread, there is no bound on the number of
threads concurrently active in the system.
 Unlimited thread could exhaust system resources like
CPU time or memory.

You might also like