Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kernel: Dealing with deadlocks in unix

A deadlock would occur if process 1 locks resource A and waits for resource B, while simultaneously (due to context switches at the "right" places) process 2 locks resource B and waits for access to resource A.

How does Unix deal with such deadlocks? I read the following here.

Many deadlocks can be prevented by simply requiring all processes that lock multiple resources to lock them in the same order (e.g., alphabetically by lock name)

How can it change the order in which locks are acquired without also changing the execution order? Could someone detail the approach to deadlock-handling taken by the modern Unix kernel?

like image 848
Anirudh Ramanathan Avatar asked Dec 12 '25 10:12

Anirudh Ramanathan


2 Answers

For Linux kernel, it does NOT handle this, because it has no idea on how to fix it. Instead, it detects this kind of deadlock at runtime and complains.

The technology it uses is lockdep, which is a runtime locking correctness validator, for details, please take a look at kernel document Documentation/lockdep-design.txt.

like image 96
Cong Wang Avatar answered Dec 15 '25 00:12

Cong Wang


No, the order can't be changed by the OS. The phrase should be read as: "if the programmer is sensible and locks resources in the same order across all competing entities, many deadlocks will be impossible".

like image 28
cnicutar Avatar answered Dec 15 '25 00:12

cnicutar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!