免费注册 查看新帖 |

Chinaunix

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

[学习分享] 一个线程退出的小问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-07-24 20:08 |只看该作者 |倒序浏览
我写了一个线程创建和退出的内核小程序,但是内核模块无法正常卸载,造成死机。求大神帮我看一下问题出在哪里?
  1. #include <linux/sched.h>
  2. #include <linux/kthread.h>
  3. #include <linux/module.h>
  4. #include <linux/err.h>
  5. #include <linux/wait.h>
  6. #include <linux/delay.h>
  7. static struct task_struct *test_task;

  8. MODULE_LICENSE("GPL");

  9. static int test_thread(void *data)
  10. {       
  11.     while(1){               
  12.             if (kthread_should_stop())
  13.                 {
  14.                         printk("The program is over!\n");
  15.                         return -1;
  16.                 }                                      
  17.         printk("This is my first kernal therad!\n");                           
  18.             mdelay(500);
  19.     }       
  20.     return 0;       
  21. }

  22. static int __init test_init_module(void)
  23. {
  24.     int err;
  25.     printk("Entering the kthread module!\n");
  26.     test_task = kthread_create(test_thread, NULL, "test_task");
  27.     if(IS_ERR(test_task)){
  28.       printk("Unable to start kernel thread./n");
  29.       err = PTR_ERR(test_task);
  30.       test_task = NULL;
  31.       return err;
  32.     }
  33.     wake_up_process(test_task);
  34.     return 0;
  35. }

  36. static void __exit test_cleanup_module(void)
  37. {
  38.     kthread_stop(test_task);
  39.     printk("Leaving the kthread module!\n");
  40. }

  41. module_init(test_init_module);
  42. module_exit(test_cleanup_module);
复制代码

论坛徽章:
72
20周年集字徽章-20	
日期:2020-10-28 14:04:30操作系统版块每日发帖之星
日期:2016-07-13 06:20:0015-16赛季CBA联赛之广夏
日期:2016-07-10 09:04:02数据库技术版块每日发帖之星
日期:2016-07-09 06:20:00操作系统版块每日发帖之星
日期:2016-07-09 06:20:00数据库技术版块每日发帖之星
日期:2016-07-07 06:20:00操作系统版块每日发帖之星
日期:2016-07-07 06:20:00操作系统版块每日发帖之星
日期:2016-07-04 06:20:00数据库技术版块每日发帖之星
日期:2016-07-03 06:20:00操作系统版块每日发帖之星
日期:2016-07-03 06:20:00数据库技术版块每日发帖之星
日期:2016-07-02 06:20:00操作系统版块每日发帖之星
日期:2016-07-02 06:20:00
2 [报告]
发表于 2016-07-27 09:04 |只看该作者
没有用过kthread_create,只用过pthread_create...

论坛徽章:
208
巨蟹座
日期:2013-09-02 09:16:36卯兔
日期:2013-09-02 20:53:59酉鸡
日期:2013-09-05 21:21:45戌狗
日期:2013-10-15 20:51:17寅虎
日期:2013-10-18 21:13:16白羊座
日期:2013-10-23 21:15:19午马
日期:2013-10-25 21:22:48技术图书徽章
日期:2013-11-01 09:11:32双鱼座
日期:2013-11-01 20:29:44丑牛
日期:2013-11-01 20:40:00卯兔
日期:2013-11-11 09:21:32酉鸡
日期:2013-12-04 19:56:39
3 [报告]
发表于 2016-07-27 09:31 |只看该作者
雪净胡天 发表于 2016-07-24 20:08
我写了一个线程创建和退出的内核小程序,但是内核模块无法正常卸载,造成死机。求大神帮我看一下问题出在哪 ...

慢点rmmod呢?
怀疑还没起来,就stop
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP