【发布时间】:2012-04-27 23:46:17
【问题描述】:
我已经看到了一些零散的信息,但我似乎无法得到一个最终答案。如何清理内核中的僵尸线程?
只是为了确保并产生处理内核中线程的最终正确方法,我想更广泛地提出这个问题。 如何在 Linux 内核中创建、终止和清理线程?
到目前为止,我所拥有的是:
thread_func:
exited = 0;
while (!must_exit)
do stuff
exited = 1;
do_exit(0)
init_module:
must_exit = 0;
exited = 1;
kthread_run(thread_func, ...) /* creates and runs the thread */
cleanup_module:
must_exit = 1;
while (!exited)
set_current_state(TASK_INTERRUPTIBLE);
msleep(1);
/* How do I cleanup? */
我发现最接近清理解决方案的是release_task,但我没有找到任何谈论它的地方。我想既然线程函数是kthread_create、kthread_run等,应该有kthread_join或kthread_wait,但没有。 do_wait 似乎也有可能,但不需要struct task_struct *。
此外,我不确定do_exit 是否是个好主意,或者是否有必要。有人可以提出如何创建、终止和清理 kthread 的最小草图吗?
【问题讨论】:
-
我好像记得有一个kthread_stop,或者kthread_should_stop,类似的东西。
-
@MartinJames,按照我的理解,你要么退出自己(使用
do_exit),要么轮询kthread_should_stop,直到有人(cleanup_module)调用kthread_stop。我没有找到任何地方说kthread_stop是否也清理了线程。让我想知道的是,如果人们(在互联网上)建议使用do_exit或其他什么,难道不应该有办法在do_exit之后清理线程吗? -
顺便说一句,this 是我说我无法得出结论性答案时所说的。那里有很多相互矛盾的东西。
标签: c multithreading linux-kernel zombie-process