免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4461 | 回复: 8
打印 上一主题 下一主题

请问内核线程怎么自己退出????????? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-17 10:55 |只看该作者 |倒序浏览
内核线程不会运行到线程函数就退出,但是我想在线程函数某个地方出错的时候就自己退出,这个怎么实现啊??

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
2 [报告]
发表于 2010-11-17 14:17 |只看该作者
回复 1# xiaopeng14


    这个错误是预知的还是不可预知的?
如果是预知的,就在预知错误的点退出就可以了

论坛徽章:
0
3 [报告]
发表于 2010-11-17 15:00 |只看该作者
是预知的,关键是我不知道用什么函数来退出。仅返回函数的话那线程本身并没有关闭。
用do_exit()??

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
4 [报告]
发表于 2010-11-17 19:56 |只看该作者
应该是do_exit

论坛徽章:
0
5 [报告]
发表于 2010-11-18 15:29 |只看该作者
但是我又有问题了 比如正常没有出现错误的话,这个线程的退出是通过其他线程调用kthread_stop发送信号到这个线程里面,然后kthread_should_stop接收到信号才退出。

这个线程我要在卸载模块的时候才退出,那么kthread_stop肯定是放在卸载函数里面。。假如在线程的内部出错,调用了do_exit(),那么该线程退出,到模块卸载的时候,会调用kthread_stop()函数,这个时候没有了线程,kthread_stop()函数会一直等待。无法正常卸载。
请问这个问题怎么解决~

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
6 [报告]
发表于 2010-11-18 16:11 |只看该作者
你先看一下kthread_stop函数的执行,应该不是一直阻塞,否则这个函数就有bug了

论坛徽章:
0
7 [报告]
发表于 2010-11-18 17:19 |只看该作者
int kthread_stop(struct task_struct *k)
{
        int ret;

        mutex_lock(&kthread_stop_lock);

        /* It could exit after stop_info.k set, but before wake_up_process. */
        get_task_struct(k);

        /* Must init completion *before* thread sees kthread_stop_info.k */
        init_completion(&kthread_stop_info.done);
        smp_wmb();

        /* Now set kthread_should_stop() to true, and wake it up. */
        kthread_stop_info.k = k;
        wake_up_process(k);
        put_task_struct(k);

        /* Once it dies, reset stop ptr, gather result and we're done. */
        wait_for_completion(&kthread_stop_info.done);
        kthread_stop_info.k = NULL;
        ret = kthread_stop_info.err;
        mutex_unlock(&kthread_stop_lock);

        return ret;
}


这个是代码,我看不出来哪里有阻塞,但是这种情况,应该就是kthread_stop没有返回,导致退出函数没有继续跑下去,使得rmmod命令也没有返回

论坛徽章:
0
8 [报告]
发表于 2010-11-18 17:21 |只看该作者
在内核代码里面,kthread_stop()函数之前还有一段注释
/**
* kthread_stop - stop a thread created by kthread_create().
* @k: thread created by kthread_create().
*
* Sets kthread_should_stop() for @k to return true, wakes it, and
* waits for it to exit.  Your threadfn() must not call do_exit()
* itself if you use this function!  This can also be called after
* kthread_create() instead of calling wake_up_process(): the thread
* will exit without calling threadfn().
*
* Returns the result of threadfn(), or %-EINTR if wake_up_process()
* was never called.
*/

表示在kthread_stop()之前不能执行do_exit().

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
9 [报告]
发表于 2010-11-18 20:41 |只看该作者
在内核代码里面,kthread_stop()函数之前还有一段注释
/**
* kthread_stop - stop a thread created by  ...
xiaopeng14 发表于 2010-11-18 17:21



    如果是这样的情况的话,你的程序退出函数,就不能调用do_exit,或者退出时发送一个信号给父进程
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP