Amit Kumar Das (Iiest Shibpur)
Amit Kumar Das (Iiest Shibpur)
Amit Kumar Das (Iiest Shibpur)
A computer system has multiple resources and those can be used by one process
at a time and for many applications we may need more than one resource at a
time. Trouble begins when we have a process (say, A) is using a resource (X) and
asking for another resource (Y) and simultaneously another process (B) using
resource (Y) and asking for X. The requirements of these two processes would
never be fulfilled and this situation is known as a deadlock.
The resources can be hardware (devices) or software (database records)
Deadlocks may be mosty relevant in the context of OS but they may occur
in other contexts applicable to a variety of concurrent systems
A
R S
B
AMIT KUMAR DAS (IIEST Shibpur) 1 / 28
RESOURCES
A deadlock is a fight between concurrent processes to aquire and use common
resources. A computer has many different resources (multiple instances of the
same resource as well, say two printers) that a process can acquire. In short, a
resource is anything that must be
acquired,
used, and
released over the course of time.
A resoucre can be preemptable – i.e., if we take it away from a process it has no
ill-effects (say a page is swapped. A non-preemptable resource cannot be taken
away without a potential failure – and we deal deadlock for non-premeptable
resource for obvious reasons.
If the resource is not available the process is automatically blocked, and awakened
when it becomes available. In other systems, denial leads to wait n a tight loop
requesting the resource, then sleeping, then trying again – note that it is as good
as blocked as no work can proceed without the requested resource. In our further
Here we assume that when a process is denied a resource request, it is put to
sleep.
AMIT KUMAR DAS (IIEST Shibpur) 2 / 28
RESOURCE AQUISITION
The following code snippets shows the chance of a deadlock for a minor variation
in the code asking for the resource. Left hand side showing a deadlock free code
and the right hand side code might lead to deadlock.
In (a) one process would acquire the 1st resource and get the 2nd as well and
carry out the job. However in (b) it may be OK sometimes but it might happen
that process A acquires resource 1 and process B acquires resource 2. Each one
will now block when trying to acquire the other one.
AMIT KUMAR DAS (IIEST Shibpur) 3 / 28
DEFINITION and DEADLOCK CONDITIONS
Now consider the motto ”Prevention is better than a cure” as we may not ignore
deadlock– costly to detect and recover and avoidance requires future knowledge.
So, if we take action not to comply one of the 4 Coffman conditions for deadlock
– we can prevent the same
i) Attacking the Mutual-Exclusion condition :
If no resource is exclusive allocated to a process then we would have no
deadlock. For example a data page can be made RO – concurrent
processes coud use it without a problem. However,
What about concurrent access to a printer – ouput would all jumbled
up and useless. However, concurrent processes may produce their
output on a spooler and the printer daemon (only) can request the
printer once the spooling is complete, But, there may be deadlock in
completing the spooling as well
For many resources exclusive access is the only choice and the strategy could be
allow exclusive access if it is absolutely essential.
ii) Attacking the Hold-and-wait condition : This is better than the 1st
condition as mutual exclusion is the key in many cases. Hold and wait can
be avoided if the requirement is that requests for all resources be made
before execution. If everything is available then only the process would be
started. The problems are
Process may not know in advancde what are the exact requirements
until started [otherwise Banker’s Algorithm would be a great success]
very poor utilisation of the resources
It may be possible in a batch system with jobs running at regular basis and the
requirements are known before hand. Hold-and-wait condition may be enforced n
a slightly different way where the process temporaraily releasing all the resoucres
before asking for more.
Even with multiple processes with this rule there cannot be any cycle in
the RAG and no deadlock.
There can be a slackened version of the rule like a process may only
ask for any high number resources from the current resource – should
it require a lower one then it should release the currently using high
numbered resources.
It may be noted that for critical resources like process-table slots, disk spooler
space, locked database records, and other abstract resources, the number of
potential resources and different uses may be so large that no ordering could
possibly work.
Condition Strategy
Mutual exclusion Spool everything
Hold and wait Request all resources before you start
Circular wait Order resources nummerically
This is similar to a scenario when two very polite persons offering each other the
right of the narrow way to cross – eager to prove his/her politeness nobody
crosses and no progress is made [though the resource – the narrow alley] is
available.
Suppose a process after a quiring a lock releases it when realising that it
could not get the next required lock. This is good to avoid deadlock.
However,
if the other process does the same at the same time [rare but possible] then
we have livelock
Consider an atomic primitive try lock in which the calling process tests a mutex
and either grabs it or returns failure. In other words, it never blocks.
Programmers can use it together with acquire lock which also tries to grab the
lock, but blocks if the lock is not available.