gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:53 29 4
gpt4 key购买 nike

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com