A deadlock is a situation where no member of some group can make progress because every other member blocks it. It can be a bit tough to understand. So, I’m going to show you an example of a deadlock graphically
Now you get the picture, deadlocks really mess up the program. Nobody can do anything because someone else is not letting them. There are many requirements to meet and confirm there is a deadlock. Those are the following:
-
Mutual exclusion: At least one resource must not be able to be shared. Otherwise, the processes would not be prevented from using the resource when necessary. So in the example above, the alley entrance is not shareable because only one vehicle can use it at the same time. It would be different if the entrance is big enough to fit two vehicles, then that resource would be shareable between two cars.
-
Hold and wait or resource holding: A process is currently holding at least one resource, waiting for more resources. A different process holds the other resources. In this case, Car B is blocking the alley entrance and is waiting for Car A to stop blocking the bathroom door.
-
No preemption: A resource won’t be released by the process holding it. Car A won’t move to let Car’s B driver to leave the bathroom.
-
Circular wait: Imagine many processes, waiting for the others to release a resource. Car A waiting for Car B to move and Car’s B driver waiting for Car A to unblock bathroom door.
To prevent deadlocks from happening, we should remove any of the previous conditions that can generate it. That is why it is important to identify all the possible situations that are likely to result in a deadlock. Make sure to account for all resources that can’t be shared and make sure that multiple threads or processes don’t try to use them at the same time.