c - 如何杀死内核模块中的等待队列?

标签 c linux-kernel kernel-module

我是内核模块的新手。使用等待队列,我阻塞线程直到缓冲区有数据。使用 hrtimer,我定期唤醒队列。现在,问题是即使在我删除内核模块之后,我仍然可以看到进程 "thread1" 仍在运行。我认为问题是等待队列永远在等待,进程在这里被阻塞了。请帮助我如何在删除模块时终止等待队列。

void thread1(void)
{
    while (thread_running) {
        ...
        wait_event_interruptible(wait_queue, does_buffer_have_data());
        ...
    }
}

最佳答案

内核线程中等待的常见方式:

void thread1(void)
{
    while(!kthread_should_stop())
    {
        ...
        wait_event_interruptible(wait_queue,
             does_buffer_have_data() || kthread_should_stop());
        if(kthread_should_stop()) break;
        ...
    }
}

void module_cleanup(void)
{
    kthread_stop(t);
}

函数kthread_should_stop检查 stop当前线程的标志。

函数kthread_stop(t)stop线程标志 t , 中断此线程执行的任何等待,并在线程完成时等待。


请注意,虽然 kthread_stop中断等待,它不会为线程设置任何未决信号

因为那个可中断的等待事件(wait_event_interruptible 等等)不返回-EINTR就在kthread_stop之后但仅重新检查条件。

因此,如果等待事件 想要在kthread_stop 之后返回,它应该检查stop在条件中明确标记

关于c - 如何杀死内核模块中的等待队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35296447/

相关文章:

linux - 为什么 open 调用需要两个参数(struct inode *、struct file*)?

c - 从 linux 驱动程序中执行二进制文件

linux - 如何安装 kernel-devel 和 kernel-headers mathching 5.x 内核?

c - '->' doesnt doesn' t 适用于结构内部的结构

使用线程的并发 TCP 服务器

c - 我无法在arduino中显示大数字

c - 如何使用 EXPORT_SYMBOL 或等效项在两个内核模块之间导出结构?

html - 如何使 Sundown 渲染 block 引用(以 ">"开头的行)

c - 使用 vmalloc 为内核模块分配大量内存

linux - 我在尝试使用命令 modprode, bash : modprobe: command not found 时收到此消息