zoukankan      html  css  js  c++  java
  • kthread_stop引起的OOP

    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. #include <linux/kthread.h>  
    2. static struct task_struct * _task;  
    3. static struct task_struct * _task2;  
    4. static struct task_struct * _task3;  
    5. static int thread_func(void *data)  
    6. {  
    7.         int j,k;  
    8.         int timeout;  
    9.         wait_queue_head_t timeout_wq;  
    10.         static int i = 0;         
    11.         i++;  
    12.         j = 0;  
    13.         k = i;  
    14.         printk("thread_func %d started/n", i);  
    15.         init_waitqueue_head(&timeout_wq);  
    16.         while(!kthread_should_stop())  
    17.         {  
    18.                 interruptible_sleep_on_timeout(&timeout_wq, HZ);  
    19.                 printk("[%d]sleeping..%d/n", k, j++);  
    20.         }  
    21.         return 0;  
    22. }  
    23. void my_start_thread(void)  
    24. {  
    25.           
    26.         //_task = kthread_create(thread_func, NULL, "thread_func2");  
    27.         //wake_up_process(_task);  
    28.         _task = kthread_run(thread_func, NULL, "thread_func2");  
    29.         _task2 = kthread_run(thread_func, NULL, "thread_func2");  
    30.         _task3 = kthread_run(thread_func, NULL, "thread_func2");  
    31.         if (!IS_ERR(_task))  
    32.         {  
    33.                 printk("kthread_create done/n");  
    34.         }  
    35.         else  
    36.         {  
    37.                 printk("kthread_create error/n");  
    38.         }  
    39. }  
    40. void my_end_thread(void)  
    41. {  
    42.         int ret = 0;  
    43.         ret = kthread_stop(_task);  
    44.         printk("end thread. ret = %d/n" , ret);  
    45.         ret = kthread_stop(_task2);  
    46.         printk("end thread. ret = %d/n" , ret);  
    47.         ret = kthread_stop(_task3);  
    48.         printk("end thread. ret = %d/n" , ret);  
    49. }  

    在执行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()的掌控之中,线程在退出时可以从容地释放资源,而不是莫名其妙地被人“暗杀”。

  • 相关阅读:
    深入探索 高效的Java异常处理框架
    Java多线程调试如何完成信息输出处理
    WebKit学习资源
    WebKit阅读起步
    Redis学习资源
    webkit在vs2008中编译
    【转】推荐几本学习MySQL的好书-MySQL 深入的书籍
    Mysql: 开启慢查询日志[ERROR] unknown variable 'log-slow-queries'处理办法
    Linux top命令详解
    eclipse svn同步过滤掉某些不需要同步的文件
  • 原文地址:https://www.cnblogs.com/GoodGoodWorkDayDayUp/p/3522842.html
Copyright © 2011-2022 走看看