免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2564 | 回复: 4
打印 上一主题 下一主题

关于内核线程的问题,请教高人 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-05 16:40 |只看该作者 |倒序浏览
用kernel_thread创建一个线程后,在该线程的中能否再次创建子进程?能否使用sys_wait4等待子线程的退出?

论坛徽章:
0
2 [报告]
发表于 2009-10-05 18:14 |只看该作者
测试程序如下, 主要想实现的功能是:主线程创建三个子线程,等待子线程处理完后退出。
但是发现主线程在创建完第一个子线程后就不继续执行了,不知道具体是什么原因,请高人给讲讲。
  1. #include <linux/config.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/kernel.h>
  5. #include <linux/sched.h>
  6. #include <asm/system.h>
  7. #include <asm/processor.h>
  8. #include <asm/signal.h>

  9. #define TIME   (10 * HZ)

  10. DECLARE_WAIT_QUEUE_HEAD(test_wait);

  11. static int parent_id = 0;        

  12. int test1(void *data)
  13. {
  14.     struct task_struct *tsk = current;

  15.     int a = 0;

  16.     DECLARE_WAITQUEUE(wait1, current);

  17.     strcpy(tsk->comm, "test");

  18.     for (;;){
  19.         add_wait_queue(&test_wait, &wait1);
  20.         set_current_state(TASK_INTERRUPTIBLE);
  21.         printk(KERN_ERR"kptest1 running.....\n");
  22.         if (a == 2)
  23.             return 0;
  24.         a++;
  25.         schedule_timeout(TIME);
  26.     }

  27.     return 0;
  28. }

  29. int parent_thread(void *data)
  30. {
  31.     int i = 0;
  32.     int g_pid[3];
  33.     struct task_struct *tsk = current;

  34.     int pid;

  35.     DECLARE_WAITQUEUE(wait, current);

  36.     strcpy(tsk->comm, "parent");

  37.     daemonize(); /*without attached user resources*/

  38.     reparent_to_init();

  39.     unblock_all_signals();


  40.        add_wait_queue(&test_wait, &wait);
  41.        set_current_state(TASK_INTERRUPTIBLE);

  42.        for (i=0; i<3; i++)
  43.        {
  44.            g_pid[i] = kernel_thread(test1, NULL,
  45.                     CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD);
  46.            if (g_pid[i] > 0)
  47.       }
  48.       printk("g_pid[] = %d,%d,%d\n", g_pid[0], g_pid[1], g_pid[2]);

  49.       i = 3;
  50.        while(i != 0)
  51.        {
  52.            int index;
  53.            pid = sys_wait4(-1, NULL, 0, NULL);
  54.            for(index=0; index < 3; index++)
  55.            {
  56.                if(pid == g_pid[index])
  57.                {
  58.                    printk("g_pid[%d]...exit\n", index);
  59.                    i--;
  60.                }
  61.            }
  62.        }
  63.    return 0;
  64. }

  65. static int __init test_init(void)
  66. {
  67.     int pid;

  68.     printk("Starting test multi_threaded...\n");
  69.     pid = kernel_thread(parent_thread, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD );
  70.     if (pid > 0) {
  71.         parent_id = pid;
  72.     }
  73.     return 0;
  74. }
  75. module_init(test_init);

  76. static void __exit test_exit(void)
  77. {
  78.     int ret;

  79.     /* Kill the thread */
  80.     ret = kill_proc(parent_id, SIGKILL, 1);

  81.     printk("test multi_threaded exit.\n");
  82. }

  83. module_exit(test_exit);
复制代码

论坛徽章:
0
3 [报告]
发表于 2009-10-08 00:10 |只看该作者
自己来结贴吧,创建线程中加上CLONE_VFORK参数,一切OK~
  1.        for (i=0; i<3; i++)

  2.        {

  3.            g_pid[i] = kernel_thread(test1, NULL,

  4.                     CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD |CLONE_VFORK );

  5.       }
复制代码

评分

参与人数 1可用积分 +3 收起 理由
liying_gg + 3 不错

查看全部评分

ryan3216 该用户已被删除
4 [报告]
发表于 2009-10-09 21:24 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2009-10-10 13:28 |只看该作者
内核是2.4.20版本,头文件是从别的地方直接复制的,没多考虑。呵呵
弱弱地问一句,从config.h怎么就看出版本低了呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP