How do you prevent deadlock in Operating System? Explain with example.


Deadlock Prevention in Operating System:

We can prevent deadlocks by ensuring that at least-one of the four necessary conditions for deadlock cannot occur, If at least one condition is not satisfied, a deadlock will net occur.

1. Mutual Exclusion:

Mutual exclusion condition must hold for non-sharable resources. For example, only one process can have access to a printer at a time, otherwise the output is disturbed. Some resources can be made sharable like a read-only file. Several processes can be granted read only access to a file without interfering with each other. However, deadlock cannot be prevented by only denying the mutual exclusion condition because some resources are intrinsically non-sharable.

2. Hold and Wait:

Deadlock can be prevented by denying the hold and wait precondition. This can be implemented in two different ways.

1. One approach is that a process requests all .the resources that it needs in one single request at process startup. The system will not grant any resource in the list until it can grant all the required resources.

2. A less restrictive approach is to allow a process to request resources only when it is currently holding no resources. If a process needs a new resource, it must first release all the resources it has and then put the request. It may include a request for reallocation of a resource it just released.

3. No Preemption:

Preemption of resources means that we take away resources from processes when they are waiting for other resources.

4. Circular Wait:

We can prevent deadlock by making circular wait impossible. We can define an order by which processes get resources to prevent circular wait. For example, each resource type is assigned a number. The processes can only get resources in increasing order of those resource numbers.

Suppose tape drive has number 1, disk drive has number 5 and printer has number 12. A process wants to read the disk drive and print out the results. It will first need to allocate the disk drive then the printer. It will be prevented from doing it in reverse order.

0 comments:

Feel free to contact the admin for any suggestions and help.