How does the linux kernel avoids deadlocks in user processes? Does it have a built-in mechanism that prevents them, or one that recognizes and kills them?
And, is there anything similar for kernel programming?
Commonly used methods for handling deadlocks within systems are (i) deadlock detection, (ii) deadlock prevention, or (iii) deadlock avoidance. Most existing static and dynamic deadlock detection tools focus on specific lock class types (e.g., pthread locks) and require an instrumentation of the code.
If you suspect a deadlock, do a ps aux | grep <exe name> , if in output, the PROCESS STATE CODE is D (Uninterruptible sleep) means it is a deadlock.
Deadlock: A situation in which a set of process exists, each of which is blocked awaiting a resource that is already allocated to another process in that set. Resource: Hardware or software entity allocated to a particular process.
Deadlock can be prevented by eliminating any of the four necessary conditions, which are mutual exclusion, hold and wait, no preemption, and circular wait. Mutual exclusion, hold and wait and no preemption cannot be violated practically. Circular wait can be feasibly eliminated by assigning a priority to each resource.
The kernel does not avoid deadlocks of user-space locks (because often it doesn't even know about them).
Deadlocks of kernel locks are avoided by writing code that is correct. This is greatly helped by lockdep, which can prove the correctness of locking operations.
(The lockdep code has been ported to user space, but it helps only for programs that bother to use it.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With