鱼C论坛

 找回密码
 立即注册
查看: 3099|回复: 3

[技术交流] kthread_stop引起的OOP

[复制链接]
发表于 2016-3-29 11:06:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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 <linux/kthread.h>  
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()的掌控之中,线程在退出时可以从容地释放资源,而不是莫名其妙地被人“暗杀”。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-4-11 16:51:41 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-4-22 10:35:41 | 显示全部楼层
好像发这些内核的没什么人顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-30 23:11:50 | 显示全部楼层
谢谢分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 06:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表