您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在内核模块代码中添加轮询功能?

如何在内核模块代码中添加轮询功能?

您可以在内核本身中找到一些很好的示例。看下一个文件

要将poll()功能添加代码中,请遵循以下步骤。

#include <linux/wait.h>
#include <linux/poll.h>
static DECLARE_WAIT_QUEUE_HEAD(fortune_wait);
static unsigned int fortune_poll(struct file *file, poll_table *wait)
{
poll_wait(file, &fortune_wait, wait);
if (new-data-is-ready)
    return POLLIN | POLLRDNORM;
return 0;
}

static const struct file_operations proc_test_fops = {
....
.poll = fortune_poll,
};

请注意,POLLIN | POLLRDNORM如果您有一些新数据要读取,并且0没有新数据要读取(poll()呼叫超时),则应返回。有关详细信息,请参见man2民意调查

wake_up_interruptible(&fortune_wait);

这是实现poll()操作的基本内容。根据您的任务,可能需要在函数中使用一些waitqueue API.read(例如wait_event_interruptible())。

其他 2022/1/1 18:14:11 有469人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶