Deadlock & Stavation
Deadlock is a condition where a set of threads are blocked because each process holding a resource is trying to access some other resources that is being held by another process eventually preventing a fair system scheduling.
A deadlock situation arises when the following four condition hold true:
1.Mutual exclusion means only one process can access a resource at a time;
2.No preemption condition means a resource can only be released voluntarily by the process holding the resource;
3.Hold & wait means a process holding resources may request additional resources held by other processes;
4.Circular wait means two or more processes are stuck in a circular chain waiting for each process to release their respective resources.
Starvation is a condition which arises when a process goes into the waiting period indefinitely as a low priority process never gets the chance to access the resource due to a constant flow of high priority processes accessing the same resource.
It is a resource management problem because a process is denied access to the resource it requires thereby pushing the process into an indefinite waiting period.
It happens because the resource it requires is never allocated to the process causing the process to starve for resources, hence the name. The best way to avoid starvation is to use the aging technique which gradually increases the priority of processes that are in the waiting period for a long time in order to ensure a fair scheduling system.
What is the difference between Deadlock and Starvation?
• Process:
• In deadlock, the two threads or processes will wait for each other and both do not proceed forward.
• In starvation, when two or more threads or processes wait for the same resource, one will roll back and let the others use the resource first and next the starving thread or process will try again . Therefore, all threads or processes will anyhow proceed forward.
• Rolling Back:
• In a deadlock, both high priority threads/processes, as well as low priority threads/processes, will wait for each other infinitely. It never ends.
• But, in a starvation, low priority ones will wait or roll back but high priority ones will proceed.
• Waiting or Lock:
• A deadlock is a circular waiting.
• A starvation is a kind of a livelock and sometimes helps to get out from a deadlock.
• Deadlock and Starvation:
• A deadlock causes starvation, but starvation does not cause a deadlock.
• Causes:
• A deadlock will occur due to mutual exclusion, hold and wait, no preemption or circular waiting.
• Starvation occurs due to scarcity of resources, uncontrolled management of resources, and process priorities.
How to prevent Deadlock?
We can prevent Deadlock by eliminating any of the above four conditions.
Eliminate Mutual Exclusion
It is not possible to dis-satisfy the mutual exclusion because some resources, such as the tape drive and printer, are inherently non-shareable.
Eliminate Hold and wait
1.Allocate all required resources to the process before the start of its execution, this way hold and wait condition is eliminated but it will lead to low device utilization. for example, if a process requires printer at a later time and we have allocated printer before the start of its execution printer will remain blocked till it has completed its execution.
2.The process will make a new request for resources after releasing the current set of resources. This solution may lead to starvation.
Eliminate No Preemption
Preempt resources from the process when resources required by other high priority processes.
Eliminate Circular Wait
Each resource will be assigned with a numerical number. A process can request the resources increasing/decreasing. order of numbering.
For Example, if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3 lesser than R5 such request will not be granted, only request for resources more than R5 will be granted.
Banker’s Algorithm
Banker’s algorithm is a deadlock avoidance algorithm, which 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.
How to stop stavation (operating system):
Aging is a technique of gradually increasing the priority of processes that wait in the system for a long time.
Race Condition 竞争:
a situation in which multiple processes read and write a shared data item and the final result depends on the relative timing of their execution.
Mutual exclusion 互斥:(Prevent Race Condition)
It is the requirement that a process can not enter its critical section while another concurrent process is currently present or executing in its critical section
Critical region 关键区域 :)
The “critical region” or “critical section” in an Operating System (OS) is a piece of code which only one process executes at a time.
Reference
http://www.differencebetween.net/technology/difference-between-deadlock-and-starvation-in-operating-system-os/
https://www.differencebetween.com/difference-between-deadlock-and-vs-starvation/
https://www.geeksforgeeks.org/deadlock-prevention/
https://www.geeksforgeeks.org/starvation-and-aging-in-operating-systems/