2

我在内核模块中使用 epoll。

我正在创建一个 kthread module_init,我想停止它module_exit

然而,这个线程几乎总是在等待epoll_wait

在我的退出函数中,即使它正在等待,我如何停止创建的线程epoll_wait

这是我的尝试,但它时不时挂起,我认为是因为中断之前没有到达线程kthread_stop

struct task_struct *thread;
bool should_stop = false;
static int __init init(void)  {
    thread = kthread_run(run, NULL, "hora");
    return 0;
}
static void __exit exit(void)  {
    l("end");

    should_stop = true;
    force_sig(SIGUSR1, thread);
    l("kthread_stop");
    kthread_stop(thread);

    l("ended");
}

int run(void *data) {
    ...
    while (true) {
        int nfd = epoll_wait(epfd, events, MAX_EVENTS, -1);
        l("got event");
        if (should_stop) {
            l("should_stop");
            break;
        }
        ...
    }
    ...
    l("closed");
    set_current_state(TASK_INTERRUPTIBLE);
    while (!kthread_should_stop()) {
        schedule();
        set_current_state(TASK_INTERRUPTIBLE);
    }
    set_current_state(TASK_RUNNING);
    l("exiting");

    return 0;
}

输出是:

end
kthread_stop
got event
should_stop
closed
exiting
ended

我已经研究了 eventfd 作为发送信号的替代方法,但它的工作量很大(只导出了几个 eventfd 函数),我不确定它是否能解决这个问题。

非常感谢任何见解和帮助。

4

0 回答 0