问题
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?
回答1:
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.
回答2:
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".
来源:https://stackoverflow.com/questions/13658645/kernel-dealing-with-deadlocks-in-unix