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

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

问题描述

我刚开始编程Linux内核线程。我有我想与你们分享一个问题。我的code是:

无效do_big_things(void *的数据)
{
   //做一些真正的大事
}
结构的task_struct * T1;
结构的task_struct * T2;无效calling_fucntion()
{
   为(J = 0; J&小于100; J ++)
   {
      T1 = kthread_run(do_big_things,&放大器; DATA1,线程1);
      T2 = kthread_run(do_big_things,&放大器; DATA2,线程2);
   }
}

现在,据我已经结束了这个问题(我可能是错的)是线程 T1 的和的 T2 的创建和内核上运行,然后程序返回在循环的开始创建和运行其他两个线程。由于没有条件等待这些线程完成,内核创建这么多线程从而导致堆栈溢出。

我要的是要知道如何使程序等待这两个线程完成,然后再回到循环开始另两个线程。

任何帮助将是AP preciated。


解决方案

/ *等待kthread_stop * /
set_current_state(TASK_INTERRUPTIBLE);
而(!kthread_should_stop()){
    时间表();
    set_current_state(TASK_INTERRUPTIBLE);
}

检查这篇文章的更多信息:睡在内核

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

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.

Any help would be appreciated.

解决方案

/* 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天全站免登陆