为何Linux内核的semaphore down()函数被标记为废弃?
down() semaphore function deprecated? Great question! Let's break down why down() was marked deprecated, and why the recommended alternatives (down_interruptible() or down_killable()) are far better choices for kernel code.
The core issue with down()
When you call down() to grab a semaphore, if the semaphore isn't available, the calling process drops into an uninterruptible sleep state (TASK_UNINTERRUPTIBLE). Here's why that's such a problem:
- Once in this state, the process won't wake up for any signal—even a fatal
SIGKILLthat would normally terminate a stuck process cold. - If the process holding the semaphore crashes, deadlocks, or gets stuck indefinitely, the process waiting via
down()will hang forever. This can lead to unresponsive system components, or even force a full system reboot to fix.
Why the alternatives are safer
The recommended functions fix this critical flaw by letting waiting processes respond to signals:
down_interruptible(): Puts the process into an interruptible sleep state (TASK_INTERRUPTIBLE). If a signal (likeSIGINTfrom Ctrl+C) hits while waiting, the process wakes up and returns the-EINTRerror code. This lets developers handle interruptions gracefully, and gives users a way to terminate stuck processes without rebooting.down_killable(): A middle-ground option that only responds to fatal signals (likeSIGKILL). This is perfect for code paths that shouldn't be interrupted by regular signals, but still need to let the system terminate the process if absolutely necessary.
The kernel community's mindset
Deprecating down() ties directly to the Linux kernel's core values of system robustness and responsiveness. Uninterruptible sleep creates unnecessary risk of system lockups, so the community pushes developers to use signal-aware alternatives that let the system recover from unexpected issues far more easily.
内容的提问来源于stack exchange,提问作者luobuda

