如何等待linux内核线程(kthread)退出? [英] How to wait for a linux kernel thread (kthread)to exit?

查看:43
本文介绍了如何等待linux内核线程(kthread)退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在 Linux 内核线程上编程.我有一个问题想和大家分享.我的代码是:

I have just started programing on Linux kernel threads. I have a problem which I would like to share with you guys. My code is:

void do_big_things(void *data)
{
   // do some really big things
} 
struct task_struct *t1;
struct task_struct *t2;

void calling_fucntion()
{
   for(j =0; j < 100; j++)
   {
      t1 = kthread_run(do_big_things, &data1, "thread1");
      t2 = kthread_run(do_big_things, &data2, "thread2");
   }
}

现在我对这个问题的结论(我可能是错的)是线程 t1t2 是由内核创建和运行的,然后程序返回在循环开始时创建并运行另外两个线程.由于没有条件等待这些线程完成,内核创建了如此多的线程从而导致堆栈溢出.

Now as far as I have concluded to this problem (I may be wrong) is that threads t1 and t2 are created and run by kernel and then program goes back at the start of loop to created and run another two threads. As there is no condition to wait for these threads to finish, the kernel creates so many threads hence causing a stack overflow.

我只想知道如何让程序等待这两个线程完成然后返回循环开始另外两个线程.

All I want is to know that how to make program wait for these two threads to finish and then go back to loop starting another two threads.

任何帮助将不胜感激.

推荐答案

/* Wait for kthread_stop */
set_current_state(TASK_INTERRUPTIBLE);
while (!kthread_should_stop()) {
    schedule();
    set_current_state(TASK_INTERRUPTIBLE);
}

查看这篇文章了解更多信息:在内核中休眠".

Check this article for more information: "Sleeping in the Kernel".

这篇关于如何等待linux内核线程(kthread)退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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