Kar*_*amy 3 c kernel-module linux-kernel
我是内核模块的新手。使用等待队列,我阻塞线程直到缓冲区有数据。使用hrtimer,我会定期唤醒队列。现在,问题是即使在我删除内核模块后,我也可以看到该进程"thread1"仍在运行。我认为问题是等待队列一直在等待并且进程在这里被阻塞。请帮助我如何在删除模块时终止等待队列。
void thread1(void)
{
while (thread_running) {
...
wait_event_interruptible(wait_queue, does_buffer_have_data());
...
}
}
Run Code Online (Sandbox Code Playgroud)
在内核线程中等待的常用方法:
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);
}
Run Code Online (Sandbox Code Playgroud)
函数kthread_should_stop检查stop当前线程的标志。
函数为 threadkthread_stop(t)设置stop标志t,中断此线程执行的任何等待,并在线程完成时等待。
请注意,当kthread_stop中断等待时,它不会为线程设置任何挂起信号。
由于可中断的等待事件(wait_event_interruptible等等)不会立即返回-EINTR,kthread_stop而只会重新检查条件。
因此,如果等待事件要在 之后返回kthread_stop,则应在 condition 中明确检查stop标志。