What is deadlock?
Deadlock is a situation when two or more processes wait for each other to finish and none of them ever finish.
- One real Life example would be: "a person says he always lies".
If he's a liar, he's telling the truth. Here truth and lie can be considered two processes that don't let each other finish
- Another example to which you all can relate. "You need experience to get a job and a job to get experience"
What are the necessary conditions for deadlock?
Mutual Exclusion: There is s resource that cannot be shared.
Hold and Wait: A process is holding at least one resource and waiting for another resource which is with some other process.
No Preemption: The operating system is not allowed to take a resource back from a process until process gives it back.
Circular Wait: A set of processes are waiting for each other in circular form.
How to break the deadlock?
The above are necessary but not sufficient conditions, even if all the conditions exist the system may or may not be in a deadlock, but if it's missing then deadlock won't happen.
It was asked in Amazon India interview round.
Happy Reading :)
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.