To know about ways to prevent deadlock we must first know the conditions that leads deadlock to occur. Deadlock occurs when we have a set of processes (not necessarily all the process in the system), each holding some resources, each requesting some resources and none of them is able to obtain what it needs i.e. to make progress. Now, there are four necessary conditions for deadlock to occur mutual exclusion, hold and wait, no preemption and circular wait. If any of these four conditions do not hold then deadlock will never occur. Let us be briefed on each of these four factors closely.
Mutual exclusion, here the resources that can be shared can always be granted simultaneous access by processes and they are never involved in a deadlock. Some of mutual exclusion resources are read only files. The processes requesting for this kinds of shareable resources will never have to wait. So, mutual exclusion must hold for non-shareable resources. However deadlock cannot be prevented by denying mutual exclusion condition. This is because some resources are non-shareable by nature like printer.
Hold and wait is a very tough condition which is very likely to create a deadlock condition in network. To avoid such condition, the system must ensure that all process that requests for a resource does not hold on to another request. Hold and wait scheme can be explained in two approaches. The first is, a process that requests for a resource and every resources it uses should be allocated before execution begins. The second is, a process can only request for a resource when it does not hold on to any other request. But the algorithms based on these approaches have very poor resource utilization.
these approaches have very poor resource utilization. This is because every resource gets locked within the processes in much early time than they are really used and they are not available for other request to use. While the second approach is very limited and is applicable only when there exist the assurance of re-usability of data on these resources. Many times these algorithm suffers from empty response as popular resources are rarely free.
No preemption condition states that resources that are allocated to processes cannot be preempted. This condition should not hold and to ensure it resources could be preempted. When a process is requested or when a process request for a resource,it allocates the resource if it's available. And if it is not then a check is made to see if the process holding the wanted resource is also waiting for additional resources. Now if both the request are not true, the process ends as the resource is neither not available nor held by other process. There are alternative approach to this. Example of CPU memory and main memory can be taken to broaden the analysis on no preemption.
Circular wait, here the resource types needs to be ordered and processed via requesting for resources in increasing order of enumeration. Here each resource type is mapped with a unique integer, this allows resource to be compared and find out the precedence order of the resources.
This were the techniques involved in preventing deadlock. However in case of deadlock, different methods are used to handle deadlock. Deadlock prevention and avoidance scheme are used to ensure that deadlock never occurs.