我是内核模块的新手。使用等待队列,我阻塞线程直到缓冲区有数据。使用 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/