如何杀死内核模块中的等待队列? [英] How to kill a wait queue in kernel module?

查看:509
本文介绍了如何杀死内核模块中的等待队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的内核模块。使用一个等待队列,我阻塞线程,直到缓冲区中有数据。使用 hrtimer ,我会定期唤醒队列中。现在,问题是即使我删除了内核模块,我可以看到这个过程线程1仍在运行。我认为这个问题是在等待队列永远等待,这个过程来到这里受阻。请帮我,我怎么能杀的等待队列,当我删除我的模块。

无效线程1(无效)
{
    而(thread_running){
        ...
        wait_event_interruptible(wait_queue,does_buffer_have_data());
        ...
    }
}


解决方案

有关在等待常用方式的内核主题:

 无效线程1(无效)
{
    而(!kthread_should_stop())
    {
        ...
        wait_event_interruptible(wait_queue,
             does_buffer_have_data()|| kthread_should_stop());
        如果(kthread_should_stop())破;
        ...
    }
}无效module_cleanup(无效)
{
    kthread_stop(T);
}

功能 kthread_should_stop 检查停止标志当前线程。

功能 kthread_stop(T)停止标志线 T ,中断而线程完成任何等待这个线程执行,并等待。


请注意,虽然 kthread_stop 中断等待,它的不设置任何未决的信号作为主题。

由于该中断的的等待事件的( wait_event_interruptible 等)不返回 -EINTR 就在 kthread_stop ,但只有重新检查的条件。

所以,如果的等待事件的希望后, kthread_stop 要返回,应该检查停止标志明确地在条件

I am new to kernel module. Using a wait queue, I am blocking the thread until the buffer has data. Using hrtimer, I am periodically waking up the queue. Now, the problem is even after I remove the kernel module, I could see that the process "thread1" is still running. I think the issue is that the wait queue is waiting forever and the process got blocked here. Please help me how can I kill the wait queue when I remove my module.

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

解决方案

Common way for wait within kernel thread:

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);
}

Function kthread_should_stop checks stop flag for current thread.

Function kthread_stop(t) sets stop flag for thread t, interrupt any waiting performed by this thread, and waits while the thread is finished.


Note, that while kthread_stop interrupts waiting, it doesn't set any pending signal for the thread.

Because of that interruptible wait for event (wait_event_interruptible and so) doesn't return -EINTR just after kthread_stop but only rechecks condition.

So, if waiting for event wants to be returned after kthread_stop, it should check stop flag explicitely in condition.

这篇关于如何杀死内核模块中的等待队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆