Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1701494
  • 博文数量: 358
  • 博客积分: 2180
  • 博客等级: 大尉
  • 技术积分: 1810
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 13:47
文章分类

全部博文(358)

文章存档

2016年(17)

2015年(55)

2014年(9)

2013年(67)

2012年(181)

2011年(29)

分类:

2012-12-05 16:29:47

原文地址:创建内核线程(2) 作者:isuxue

1.       头文件

#include    //wake_up_process()

#include //kthread_create()、kthread_run()

#include               //IS_ERR()、PTR_ERR()

2.       实现

2.1创建线程

在模块初始化时,可以进行线程的创建。使用下面的函数和宏定义:

struct task_struct *kthread_create(int (*threadfn)(void *data),

                            void *data,

                            const char namefmt[], ...);

#define kthread_run(threadfn, data, namefmt, ...)                     \

({                                                            \

    struct task_struct *__k                                        \

           = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \

    if (!IS_ERR(__k))                                        \

           wake_up_process(__k);                                \

    __k;                                                     \

})

例如:

static struct task_struct *test_task;

static int test_init_module(void)

{

    int err;

    test_task = kthread_create(test_thread, NULL, "test_task");

    if(IS_ERR(test_task)){

      printk("Unable to start kernel thread.\n");

      err = PTR_ERR(test_task);

      test_task = NULL;

      return err;

    }

wake_up_process(test_task);
       return 0;
   }

   module_init(test_init_module);

2.2线程函数

在线程函数里,完成所需的业务逻辑工作。主要框架如下所示:

int threadfunc(void *data){

        …

        while(1){

               set_current_state(TASK_UNINTERRUPTIBLE);

               if(kthread_should_stop()) break;

               if(){//条件为真

                      //进行业务处理

               }

               else{//条件为假

                      //让出CPU运行其他线程,并在指定的时间内重新被调度

                      schedule_timeout(HZ);

               }

        }

        …

        return 0;

}

2.3结束线程

在模块卸载时,可以结束线程的运行。使用下面的函数:

int kthread_stop(struct task_struct *k);

例如:

              static void test_cleanup_module(void)

{

            if(test_task){

                kthread_stop(test_task);

                test_task = NULL;

            }

}

module_exit(test_cleanup_module);

3.       注意事项

(1)       在调用kthread_stop函数时,线程函数不能已经运行结束。否则,kthread_stop函数会一直进行等待。

(2)       线程函数必须能让出CPU,以便能运行其他线程。同时线程函数也必须能重新被调度运行。在例子程序中,这是通过schedule_timeout()函数完成的。

4.性能测试

可以使用top命令来查看线程(包括内核线程)的CPU利用率。命令如下:

       top –p 线程号

可以使用下面命令来查找线程号:

       ps aux|grep 线程名


可以用下面的命令显示所有内核线程:
      ps afx

       注:线程名由kthread_create函数的第三个参数指定

阅读(789) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~