c - 如何让字符设备驱动程序内核模块的 write 调用者进入休眠状态

标签 c linux-kernel blocking

我在 Linux 中创建了一个字符设备驱动程序内核模块,如 this fabulous tutorial 中所述。 。驱动程序运行良好,但我需要扩展内核模块功能

static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t *off)

以便调用用户进程(调用函数 write() )被阻塞一段固定的时间。

我可以包含 linux/delay.h 并在内核模块的函数 device_write 中使用 msleep(1000) 让 CPU等待。但我不知道这是否是阻止用户空间中 write() 函数调用的正确方法。

请不要将此与对资源的并发访问混淆。这不是我的重点。我只想阻止用户空间程序的 write() 调用,直到将数据写入我的硬件设备,这需要一些时间。只有在此之后,我才知道操作是否成功,并且我可以通过 device_write 返回适当的状态调用给用户空间程序的 write 调用。

最佳答案

您可以在阻塞函数内使用 down_interruptible(struct semaphore),并在其他内核线程/事件处理程序中释放信号量,从而释放调用线程。

关于c - 如何让字符设备驱动程序内核模块的 write 调用者进入休眠状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37911568/

相关文章:

c - 用C存储和处理大量数据

c - 如何将 C 代码制作成 MISRA C :2012 compliance?

c - 在 C 中打印用循环填充的字符串?

memory-management - 页面结构的有效载荷在哪里

linux - 如何在 Linux 中删除虚拟接口(interface)?

python - 在 Python 中非阻塞地收集子进程输出

c - 无法在 xml 中获取 Soap 响应的根节点

c - virt_to_bus() 在 linux 内核模块编译中被弃用

ruby - 像使用 Ajax 一样使用 Ruby 调用异步方法

python - 服务器使用 Flask/Redis 发送事件 : how can more than one client view a stream?