You are on page 1of 6

OPERATING SYSTEM

ACTIVITY - 05

1. Study probable conditions for deadlock occurrence and how to


overcome it.

B
Deadlock

R
Deadlock is a situation which involves the interaction of more than one
resource and processes with each other. We can visualise the occurrence of
TH
deadlock as a situation where there are two people on a staircase. One is
ascending the staircase while the other is descending.
The staircase is so narrow that it can only fit one person at a time. As a result,
one has to retreat while the other moves on and uses the staircase. Once that
person has finished, the other one can use that staircase. But here, neither
U

person is willing to retreat and waits for the other to retreat.


None of them are able to use the staircase. The people here are the
processes, and the staircase is the resource.
R

When a process requests for the resource that is being held by another
process which needs another resource to continue, but is being held by the
AM

first process, then it is called a deadlock. There are four conditions necessary
for the occurrence of a deadlock.
They can be understood with the help of the above illustrated example of
staircase:

1. Mutual Exclusion: When two people meet in the landings, they can’t
just walk through because there is space only for one person. This
condition allows only one person (or process) to use the step between
them (or the resource) is the first condition necessary for the occurrence
of the deadlock.
2. Hold and Wait: When the two people refuse to retreat and hold their
ground, it is called holding. This is the next necessary condition for
deadlock.

3. No Preemption: For resolving the deadlock one can simply cancel one
of the processes for others to continue. But the Operating System
doesn’t do so. It allocates the resources to the processors for as much
time as is needed until the task is completed. Hence, there is no
temporary reallocation of the resources. It is the third condition for
deadlock.

4. Circular Wait: When the two people refuse to retreat and wait for each

B
other to retreat so that they can complete their task, it is called circular
wait. It is the last condition for deadlock to occur.

R
Note: All four conditions are necessary for deadlock to occur. If any single one
is prevented or resolved, the deadlock is resolved.
TH
How to OVERCOME a deadlock

There are two approaches of overcoming a Deadlock:


U

1. Process Termination:
R

To eliminate the deadlock, we can simply kill one or more processes. For this,
we use two methods:
AM

● (a) Abort all the Deadlocked Processes:


Aborting all the processes will certainly break the deadlock, but with a
great expense. The deadlocked processes may have computed for a
long time and the result of those partial computations must be discarded
and there is a probability to recalculate them later.

● (b) Abort one process at a time until deadlock is eliminated:


Abort one deadlocked process at a time, until the deadlock cycle is
eliminated from the system.
Due to this method, there may be considerable overhead, because
after aborting each process, we have to run a deadlock detection
algorithm to check whether any processes are still deadlocked.
2. Resource Preemption:

To eliminate deadlocks using resource preemption, we preempt some


resources from processes and give those resources to other processes. This
method will raise three issues –

● (a) Selecting a victim:


We must determine which resources and which processes are to be
preempted and also the order to minimise the cost.

● (b) Rollback:
We must determine what should be done with the process from which

B
resources are preempted. One simple idea is total rollback. That means
abort the process and restart it.

R
● (c) Starvation:
In a system, it may happen that the same process is always picked as a
victim. As a result, that process will never complete its designated task.
TH
This situation is called Starvation and must be avoided. One solution is
that a process must be picked as a victim only a finite number of times.
U
R
AM
2. Identify relationships between threads and processes.

Process

Processes are basically the programs that are dispatched from the ready state
and are scheduled in the CPU for execution. PCB(Process Control Block)
holds the concept of process. A process can create other processes which are
known as Child Processes. The process takes more time to terminate and it is
isolated, meaning it does not share the memory with any other process.
The process can have the following states new, ready, running, waiting,

B
terminated, and suspended.

Thread

R
Thread is the segment of a process which means a process can have
multiple threads and these multiple threads are contained within a process. A
TH
thread has three states: Running, Ready, and Blocked.
The thread takes less time to terminate as compared to the process but unlike
the process, threads do not isolate.
U

Difference between Process and Thread


R

S.NO Process Thread


AM

Process means any Thread means a segment of a


1.
program is in execution. process.

The process takes more The thread takes less time to


2.
time to terminate. terminate.

It takes more time for


3. It takes less time for creation.
creation.

It also takes more time for It takes less time for context
4.
context switching. switching.
The process is less
Thread is more efficient in terms of
5. efficient in terms of
communication.
communication.

We don’t need multi programs in


Multiprogramming holds
action for multiple threads because a
6. the concepts of
single process consists of multiple
multi-process.
threads.

7. The process is isolated. Threads share memory.

B
A Thread is lightweight as each
The process is called the
8. thread in a process shares code,
heavyweight process.
data, and resources.

R
Process switching uses Thread switching does not require
9. an interface in an calling an operating system and
operating system. causes an interrupt to the kernel.
TH
If one process is blocked
If a user-level thread is blocked, then
then it will not affect the
10. all other user-level threads are
execution of other
blocked.
U

processes

The process has its own


R

Thread has Parents’ PCB, its own


Process Control Block,
11. Thread Control Block, and Stack and
Stack, and Address
common Address space.
Space.
AM

Since all threads of the same process


Changes to the parent share address space and other
12. process do not affect child resources, any changes to the main
processes. thread may affect the behaviour of the
other threads of the process.

A system call is involved No system call is involved, it is


13.
in it. created using APIs.
3. Comprehend the differences between types of threads

Types of Threads

1. User Level thread (ULT)


Is implemented in the user level library, they are not created using the
system calls. Thread switching does not need to call OS and to cause
interrupt to Kernel. Kernel doesn’t know about the user level thread and
manages them as if they were single-threaded processes.

B
● Advantages of ULT
● Can be implemented on an OS that doesn’t support
multithreading.

R
● Simple representation since thread has only program
counter, register set, stack space.
● Simple to create since no intervention of kernel
TH
● Limitations of ULT
● No or less co-ordination among the threads and Kernel.
● If one thread causes a page fault, the entire process
blocks.
U

2. Kernel Level Thread (KLT)


Kernel knows and manages the threads. Instead of a thread table in
R

each process, the kernel itself has a thread table (a master one) that
keeps track of all the threads in the system. In addition, the kernel also
AM

maintains the traditional process table to keep track of the processes


.
● Advantages of KLT
● Since the kernel has full knowledge about the threads in
the system, the scheduler may decide to give more time to
processes having a large number of threads.
● Good for applications that frequently block.

● Limitations of KLT –
● Slow and inefficient.
● It requires a thread control block so it is an overhead.

You might also like