Understanding Deadlock in Operating Systems: Causes, Prevention, and Detection, Study notes of Computer Science

In computer science, deadlock refers to a specific condition when two or more processes are each waiting for another to release a resource, or more than two processes are waiting for resources in a circular chain (see Necessary conditions).

Typology: Study notes

2020/2021

Uploaded on 11/07/2019

sushil-bohaju
sushil-bohaju 🇳🇵

3 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A REPORT ON
DEADLOCK ON OPERATING SYSYTEM
AND
IT’S PREVENTION
Submitted To
Umang Pandey
Lecturer (Computer Organization)
Submitted by
Sushil Bohaju
Roll no: 161114
September 10, 2019
Lalitpur
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Deadlock in Operating Systems: Causes, Prevention, and Detection and more Study notes Computer Science in PDF only on Docsity!

A REPORT ON

DEADLOCK ON OPERATING SYSYTEM

AND

IT’S PREVENTION

Submitted To

Umang Pandey

Lecturer (Computer Organization)

Submitted by

Sushil Bohaju

Roll no: 161114

September 10, 2019

Lalitpur

Table of Contents

  • CHAPTER I .................................................................................................................. Table of Contents ...........................................................................................................i
  • INTRODUCTION ........................................................................................................
  • CHAPTER II ................................................................................................................
  • PREVENTENTION OF DEADLOCK
    • 2.1 Mutual exclusion..................................................................................................
    • 2.2 Hold and wait.......................................................................................................
    • 2.3 No preemption......................................................................................................
    • 2.4 Circular.................................................................................................................
  • CHAPTER III ...............................................................................................................
  • Deadlock detection and recovery ................................................................................
    • 3.1 For Resource.........................................................................................................
      • 3.1.1 Preempt the resource......................................................................................
      • 3.1.2 Rollback to a safe state..................................................................................
    • 3.2 For Process...........................................................................................................
      • 3.2.1 Kill a process.................................................................................................
      • 3.2.2 Kill all process...............................................................................................
  • CHAPTER IV ...............................................................................................................
  • BANKERS ALGORITHMS ........................................................................................
    • 4.1 Safety Algorithm..................................................................................................
    • 4.2 Resource-Request Algorithm................................................................................
  • CHAPTER V
  • CONCLUSION .............................................................................................................
  • REFERENCES ...........................................................................................................

PREVENTENTION OF DEADLOCK

Deadlock can be prevent by avoiding the necessary condition for deadlock. The necessary condition to appear deadlock are as follows:

  • Mutual exclusion
  • Hold and wait
  • No preemption
  • Circular wait

For deadlock these four condition are necessary to happen. If any one of these condition are not appear, there will be no deadlock further.

2.1 Mutual exclusion

One or more than one resource are non-sharable (Only one process can use at a time). From the resource point of view is the fact that a resource can never be used by more than one process simultaneously which is fair enough but that is the main reason behind the deadlock. If a resource could have been used by more than one process at the same time then the process would have never been waiting for any resource.

However, if we can be able to violate resources behaving in the mutually exclusive manner then the deadlock can be prevented.

For a device like printer, spooling can work. There is a memory associated with the printer which stores jobs from each of the process into it. Later, Printer collects all the jobs and print each one of them according to FCFS. By using this mechanism, the process doesn't have to wait for the printer and it can continue whatever it was doing. Later, it collects the output when it is produced.

Figure 2(a): Mutual exclusion

Although, Spooling can be an effective approach to violate mutual exclusion but it suffers from two kinds of problems.

  1. This cannot be applied to every resource.
  2. After some point of time, there may arise a race condition between the processes to get space in that spool.

We cannot force a resource to be used by more than one process at the same time since it will not be fair enough and some serious problems may arise in the performance. Therefore, we cannot violate mutual exclusion for a process practically.

2.2 Hold and wait

Hold and wait condition lies when a process holds a resource and waiting for some other resource to complete its task. Deadlock occurs because there can be more than one process which are holding one resource and waiting for other in the cyclic order. A process must be assigned all the necessary resources before the execution starts. A process must not wait for any resource once the execution has been started. i.e. If the process request all the resources before starting the execution, or each process can request resources only when it does not hold any resources, deadlock can be eliminated.

!(Hold and wait) = !hold or !wait (negation of hold and wait is, either you don't hold or you don't wait) This can be implemented practically if a process declares all the resources initially. However, this sounds very practical but can't be done in the computer system because a process can't determine necessary resources initially. Process is the set of instructions which are executed by the CPU. Each of the instruction may demand multiple resources at the multiple times. The need cannot be fixed by the OS. The problem with the approach is:

  1. Practically not possible.
  2. Possibility of getting starved will be increases due to the fact that some process may hold a resource for a very long time.

2.3 No preemption

A resource cannot be taken from a process unless the process releases the resource.If a process that is holding some resources request another resources and that resources cannot be allocated to it , then it must release all resources that currently allocated to it.

Consider a printer is being used by any process. If we take the printer away from that process and assign it to some other process then all the data which has been printed can become inconsistent and ineffective and also the fact that the process can't start printing again from where it has left which causes performance inefficiency.

2.4 Circular

The circular wait condition states that there exists a chain of processes where each process is waiting for a resource that is being held by another process. In this condition it does not necessarily mean that the process is holding onto a resource that the other is requesting, but rather that all processes are waiting for a resource that is being held by another process.

To violate circular wait, we can assign a priority number to each of the resource. A process can't request for a lesser priority resource. This ensures that not a single

Figure 3(a): Deadlock Detection

In single instanced resource types, if a cycle is being formed in the system then there will definitely be a deadlock. On the other hand, in multiple instanced resource type graph, detecting a cycle is not just enough. We have to apply the safety algorithm on the system by converting the resource allocation graph into the allocation matrix and request matrix.

In order to recover the system from deadlocks, either OS considers resources or processes.

3.1 For Resource

3.1.1 Preempt the resource

We can snatch one of the resources from the owner of the resource (process) and give it to the other process with the expectation that it will complete the execution and will release this resource sooner. Well, choosing a resource which will be snatched is going to be a bit difficult.

3.1.2 Rollback to a safe state

System passes through various states to get into the deadlock state. The operating system can roll back the system to the previous safe state. For this purpose, OS needs to implement check pointing at every state.

The moment, we get into deadlock, we will rollback all the allocations to get into the previous safe state.

3.2 For Process

3.2.1 Kill a process

Killing a process can solve our problem but the bigger concern is to decide which process to kill. Generally, Operating system kills a process which has done least amount of work until now.

3.2.2 Kill all process

This is not a suggestible approach but can be implemented if the problem becomes very serious. Killing all process will lead to inefficiency in the system because all the processes will execute again from starting.

Figure 3(b): Recovery of Deadlock

CHAPTER IV

BANKERS ALGORITHMS

The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.

Consider there are n account holders in a bank and the sum of the money in all of their accounts is S. Every time a loan has to be granted by the bank, it subtracts the loan amount from the total money the bank has. Then it checks if that difference is greater than S. It is done because, only then, the bank would have enough money even if all the n account holders draw all their money at once.

Banker’s algorithm works in a similar way in computers. Whenever a new process is created, it must exactly specify the maximum instances of each resource type that it needs.

Following Data structures are used to implement the Banker’s Algorithm:

Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.

Available:

  • It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
  1. If Requesti <= Needi Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum claim.

  2. If Requesti <= Available Goto step (3); otherwise, P (^) i must wait, since the resources are not available.

  3. Have the system pretend to have allocated the requested resources to process Pi by modifying the state as follows: Available = Available – Requesti Allocationi = Allocationi + Requesti Needi = Needi– Request (^) i

CHAPTER V

CONCLUSION

A deadlock state occurs when two or more processes are waiting indefinitely for an event that can be caused only by one of the waiting processes. There are three principal methods for dealing with deadlocks:

  • Use some protocol to prevent or avoid deadlocks, ensuring that the system, will never enter a deadlock state.
  • Allow the system to enter a deadlock state, detect it, and then recover.
  • Ignore the problem altogether and pretend that deadlocks never occur in the system. The third solution is the one used by most operating systems, including UNIX and Windows A deadlock can occur only if four necessary conditions hold simultaneously in the system: mutual exclusion, hold and wait, no preemption, and circular wait. To prevent deadlocks, we can ensure that at least one of the necessary conditions never holds. A method for avoiding deadlocks that is less stringent than the prevention algorithms requires that the operating system have a priori information on how each process will utilize system resources.

REFERENCES

Abraham Silberschatz, Greg Gagne, and Peter Baer Galvin, " Operating System

Concepts, Ninth Edition

http://www.padakuu.com/article/191-deadlock-summary

https://www.gatevidyalay.com/bankers-algorithm-deadlock-avoidance-2/

https://www.geeksforgeeks.org/bankers-algorithm-in-operating-system-2/

https://www.javatpoint.com/os-deadlock-detection-and-recovery