Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1293428
  • 博文数量: 175
  • 博客积分: 2743
  • 博客等级: 少校
  • 技术积分: 4024
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-30 01:41
文章分类

全部博文(175)

文章存档

2015年(1)

2013年(53)

2012年(71)

2011年(50)

分类: LINUX

2011-12-14 14:56:33

1 使用kthread_create创建线程:
    struct task_struct *kthread_create(int (*threadfn)(void *data),
                                            void *data,
                                            const char *namefmt, ...);
这个函数可以像printk一样传入某种格式的线程名
线程创建后,不会马上运行,而是需要将kthread_create() 返回的task_struct指针传给wake_up_process(),然后通过此函数运行线程。
2. 当然,还有一个创建并启动线程的函数:kthread_run
   struct task_struct *kthread_run(int (*threadfn)(void *data),
                                    void *data,
                                    const char *namefmt, ...);
3. 线程一旦启动起来后,会一直运行,除非该线程主动调用do_exit函数,或者其他的进程调用kthread_stop函数,结束线程的运行。
    int kthread_stop(struct task_struct *thread);
kthread_stop() 通过发送信号给线程。
如果线程函数正在处理一个非常重要的任务,它不会被中断的。当然如果线程函数永远不返回并且不检查信号,它将永远都不会停止。
参考:Kernel threads made easy
--
  1. view plaincopy to clipboardprint?
  2. #include <linux/kthread.h>
  3. static struct task_struct * _task;
  4. static struct task_struct * _task2;
  5. static struct task_struct * _task3;
  6. static int thread_func(void *data)
  7. {
  8.         int j,k;
  9.         int timeout;
  10.         wait_queue_head_t timeout_wq;
  11.         static int i = 0;
  12.         i++;
  13.         j = 0;
  14.         k = i;
  15.         printk("thread_func %d started\n", i);
  16.         init_waitqueue_head(&timeout_wq);
  17.         while(!kthread_should_stop())
  18.         {
  19.                 interruptible_sleep_on_timeout(&timeout_wq, HZ);
  20.                 printk("[%d]sleeping..%d\n", k, j++);
  21.         }
  22.         return 0;
  23. }
  24. void my_start_thread(void)
  25. {
  26.           
  27.         //_task = kthread_create(thread_func, NULL, "thread_func2");
  28.         //wake_up_process(_task);
  29.         _task = kthread_run(thread_func, NULL, "thread_func2");
  30.         _task2 = kthread_run(thread_func, NULL, "thread_func2");
  31.         _task3 = kthread_run(thread_func, NULL, "thread_func2");
  32.         if (!IS_ERR(_task))
  33.         {
  34.                 printk("kthread_create done\n");
  35.         }
  36.         else
  37.         {
  38.                 printk("kthread_create error\n");
  39.         }
  40. }
  41. void my_end_thread(void)
  42. {
  43.         int ret = 0;
  44.         ret = kthread_stop(_task);
  45.         printk("end thread. ret = %d\n" , ret);
  46.         ret = kthread_stop(_task2);
  47.         printk("end thread. ret = %d\n" , ret);
  48.         ret = kthread_stop(_task3);
  49.         printk("end thread. ret = %d\n" , ret);
  50. }
  51. #include <linux/kthread.h>
  52. static struct task_struct * _task;
  53. static struct task_struct * _task2;
  54. static struct task_struct * _task3;
  55. static int thread_func(void *data)
  56. {
  57.         int j,k;
  58.         int timeout;
  59.         wait_queue_head_t timeout_wq;
  60.         static int i = 0;
  61.         i++;
  62.         j = 0;
  63.         k = i;
  64.         printk("thread_func %d started\n", i);
  65.         init_waitqueue_head(&timeout_wq);
  66.         while(!kthread_should_stop())
  67.         {
  68.                 interruptible_sleep_on_timeout(&timeout_wq, HZ);
  69.                 printk("[%d]sleeping..%d\n", k, j++);
  70.         }
  71.         return 0;
  72. }
  73. void my_start_thread(void)
  74. {
  75.        
  76.         //_task = kthread_create(thread_func, NULL, "thread_func2");
  77.         //wake_up_process(_task);
  78.         _task = kthread_run(thread_func, NULL, "thread_func2");
  79.         _task2 = kthread_run(thread_func, NULL, "thread_func2");
  80.         _task3 = kthread_run(thread_func, NULL, "thread_func2");
  81.         if (!IS_ERR(_task))
  82.         {
  83.                 printk("kthread_create done\n");
  84.         }
  85.         else
  86.         {
  87.                 printk("kthread_create error\n");
  88.         }
  89. }
  90. void my_end_thread(void)
  91. {
  92.         int ret = 0;
  93.         ret = kthread_stop(_task);
  94.         printk("end thread. ret = %d\n" , ret);
  95.         ret = kthread_stop(_task2);
  96.         printk("end thread. ret = %d\n" , ret);
  97.         ret = kthread_stop(_task3);
  98.         printk("end thread. ret = %d\n" , ret);
  99. }
  100. /*在执行kthread_stop的时候,目标线程必须没有退出,否则会Oops。原因很容易理解,当目标线程退出的时候,其对应的task结构也变得无效,kthread_stop引用该无效task结构就会出错。
  101. 为了避免这种情况,需要确保线程没有退出,其方法如代码中所示:
  102. */
  103. thread_func()
  104. {
  105.     // do your work here
  106.     // wait to exit
  107.     while(!thread_could_stop())
  108.     {
  109.            wait();
  110.     }
  111. }
  112. exit_code()
  113. {
  114.      kthread_stop(_task); //发信号给task,通知其可以退出了
  115. }
  116. //这种退出机制很温和,一切尽在thread_func()的掌控之中,线程在退出时可以从容地释放资源,而不是莫名其妙地被人“暗杀”。

阅读(1564) | 评论(0) | 转发(0) |
0

上一篇:kthread_run

下一篇:class_create&device_create_file

给主人留下些什么吧!~~