OS T-Assignment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Federal Urdu University Of Arts,

Science and Technology

Syeda Aliya Tanveer

Syed Tanveer Hussain

30439

BSCS 5 A

OS Theory

Sir Saeed Ullah

2/6/2022
Assignment no. 3
How Linux/Unix deals with the Deadlock?
Deadlock:
A deadlock is a situation in which two computer programs
sharing the same resource are effectively preventing each other
from accessing the resource, resulting in both programs ceasing
to function.
So we can say : a deadlock occurs when two separate processes
compete for resources held by one another.
Example:
For example, consider the following processing sequence for
two concurrently executing application programs:
Program 1 Program 2

Update Table B/Page 1 Update Table A/Page 1


Lock established Lock established
Intermediate processing Intermediate processing
Update Table A/Page 1 Update Table B/Page 1
Lock (wait) DEADLOCK Lock (wait)
A deadlock occurs when
Program 1 requests a lock for a data page held by Program 2 , &
Program 2 requests a lock for a data page held by Program 1.
Both programs are waiting on the other to finish before they can
proceed.
Necessary conditions for deadlock :
There are four necessary conditions that must hold
simultaneously for a deadlock to occur.
1. Mutual Exclusion: At least one resource must be held in
non-sharable mode. only one process at a time can use the
resource. if another process requests that resource the requesting
process must be delayed until the resource has been released.
2. Hold and Wait: A process must be holding at least one
resource and waiting to acquire additional resources that are
currently being held by other process.
3. No preemption: Resource can not be preempted. A process
can not use the resource forcefully which is acquired by other
processes.
4. Circular wait: A circular chain of hold and wait condition
exists in the system. There is a cycle of threads, each of which is
waiting for the next to release a resource before it can continue.
Situations/Scenarios:
There are two scenario for deadlock in Linux are:
Scenario 1: Self deadlock - "re-acquire lock":
Say there's a thread A, it acquires lock X1 and then before
relinquishing the lock, it re-acquires it. This will lead to a
deadlock.
thread_A()
{
...
spinlock_lock(&X1)
do_some_stuff()
spinlock_lock(&X1) //deadlock!!
...
spinlock_unlock(&X1)
}
Scenario 2: ABBA deadlock
Say there're two threads thread A and thread B executing
simultaneously and two locks X1 and X2 (protecting some
critical region). A acquires X1 and B acquires X2 and then A
tries to acquire X2 and B tries to acquire X1. This will deadlock
the both the threads.
thread_A()
{
...
spinlock_lock(&X1)
do_some_stuff()
spinlock_lock(&X2) //deadlock if thread_B has already acquired
X2 as well as X1
...
}
thread_B()
{
...
spinlock_lock(&X2)
do_some_stuff()
spinlock_lock(&X1) //deadlock if thread_A has already acquired
X1 as well as X2
...
}
Unix deals with the Deadlock:
The UNIX locking features identify and avoid deadlocks. If
process 1 locks resource A and waits for resource B, while
process 2 locks resource B and waits for access to resource A
(due to context shifts in the "correct" locations), a deadlock
occurs. Many deadlocks may be avoided if all processes that
lock multiple resources do so in the same sequence (e.g.,
alphabetically by lock name).
Linux deals with the Deadlock:
For Linux kernel, it does NOT handle this, since it has no
knowledge how to correct it. Instead, it identifies and reports
this type of deadlock during execution. It makes use of lockdep,
a runtime locking correctness checker.

You might also like