linux-kernel - 我应该在Linux内核开发中使用哪种互斥锁变量?

标签 linux-kernel mutex semaphore

AFAIK是在编写LDD3(第3版Linux设备驱动程序)之后将互斥量API引入内核的,因此在本书中没有进行介绍。

这本书描述了如何使用内核的信号量API来实现互斥功能。

建议使用down_interruptable()而不是down():

You do not, as a general rule, want to use noninterruptible operations unless there truly is no alternative. Non-interruptible operations are a good way to create unkillable processes (the dreaded “D state” seen in ps), and annoy your users [Linux Device Drivers 3rd ed]



现在。这是我的问题:

互斥量API具有两个“相似”功能:mutex_lock_killable()mutex_lock_interruptable()。我应该选择哪一个?

最佳答案

使用mutex_lock_interruptible()函数可以使您的驱动程序被任何信号中断。
这意味着应该编写系统调用,以便可以重新启动它。
(另请参见ERESTARTSYS。)

使用mutex_lock_killable()允许您的驱动程序仅被实际上杀死该进程的信号打断,即该进程没有机会查看系统调用的结果,甚至无法重试。

当您可以确保互斥量不会长时间保留时,请使用mutex_lock()

关于linux-kernel - 我应该在Linux内核开发中使用哪种互斥锁变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18298962/

相关文章:

c - local_fiq_enable() 的功能

linux - 什么时候在内核中创建 kallsyms?

process - 如何监控容器内的进程?

android - 这个内核源码能用吗?

c++ - 递归和非递归锁 (Mutex)

c++ - 错误 C2248 : 'X::operator =' : cannot access private member declared in class 'X'

go - 将标准Golang映射转换为Sync.Map以避免出现竞争情况

python - 线程应用程序的信号量同步失败,Python

c - 父进程在子进程之前执行,如何通过信号量强制相反?

快速信号量命令行工具项目等待组完成