Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2691453
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2514
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类: LINUX

2013-04-27 20:38:12

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函数的第三个参数指定

 

*****************************************************************************************************

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

 

代码

  #include    
 static struct task_struct * _task;   
 static struct task_struct * _task2;   
 static struct task_struct * _task3;   
 static int thread_func(void *data)   
 {   
         int j,k;   
         int timeout;   
         wait_queue_head_t timeout_wq;   
         static int i = 0;          
         i++;   
         j = 0;   
         k = i;   
         printk("thread_func %d started\n", i);   
         init_waitqueue_head(&timeout_wq);   
         while(!kthread_should_stop())   
         {   
                 interruptible_sleep_on_timeout(&timeout_wq, HZ);   
                 printk("[%d]sleeping..%d\n", k, j++);   
         }   
         return 0;   
 }   
 void my_start_thread(void)   
 {   
            
         //_task = kthread_create(thread_func, NULL, "thread_func2");   
         //wake_up_process(_task);   
         _task = kthread_run(thread_func, NULL, "thread_func2");   
         _task2 = kthread_run(thread_func, NULL, "thread_func2");   
         _task3 = kthread_run(thread_func, NULL, "thread_func2");   
         if (!IS_ERR(_task))   
         {   
                 printk("kthread_create done\n");   
         }   
         else  
         {   
                 printk("kthread_create error\n");   
         }   
 }   
 void my_end_thread(void)   
 {   
         int ret = 0;   
         ret = kthread_stop(_task);   
         printk("end thread. ret = %d\n" , ret);   
         ret = kthread_stop(_task2);   
         printk("end thread. ret = %d\n" , ret);   
         ret = kthread_stop(_task3);   
         printk("end thread. ret = %d\n" , ret);   
 }

在执行kthread_stop的时候,目标线程必须没有退出,否则会Oops。原因很容易理解,当目标线程退出的时候,其对应的task结构也变得无效,kthread_stop引用该无效task结构就会出错。

为了避免这种情况,需要确保线程没有退出,其方法如代码中所示:

thread_func()

{

    // do your work here

    // wait to exit

    while(!thread_could_stop())

    {

           wait();

    }

}

exit_code()

{

     kthread_stop(_task);   //发信号给task,通知其可以退出了

}

这种退出机制很温和,一切尽在thread_func()的掌控之中,线程在退出时可以从容地释放资源,而不是莫名其妙地被人“暗杀”。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/maray/archive/2009/08/13/4442450.aspx

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