xiaopeng14 发表于 2010-11-17 10:55

请问内核线程怎么自己退出?????????

内核线程不会运行到线程函数就退出,但是我想在线程函数某个地方出错的时候就自己退出,这个怎么实现啊??

dreamice 发表于 2010-11-17 14:17

回复 1# xiaopeng14


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

xiaopeng14 发表于 2010-11-17 15:00

是预知的,关键是我不知道用什么函数来退出。仅返回函数的话那线程本身并没有关闭。
用do_exit()??

dreamice 发表于 2010-11-17 19:56

应该是do_exit

xiaopeng14 发表于 2010-11-18 15:29

但是我又有问题了 比如正常没有出现错误的话,这个线程的退出是通过其他线程调用kthread_stop发送信号到这个线程里面,然后kthread_should_stop接收到信号才退出。

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

dreamice 发表于 2010-11-18 16:11

你先看一下kthread_stop函数的执行,应该不是一直阻塞,否则这个函数就有bug了

xiaopeng14 发表于 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命令也没有返回

xiaopeng14 发表于 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().

dreamice 发表于 2010-11-18 20:41

在内核代码里面,kthread_stop()函数之前还有一段注释
/**
* kthread_stop - stop a thread created by...
xiaopeng14 发表于 2010-11-18 17:21 http://linux.chinaunix.net/bbs/images/common/back.gif


    如果是这样的情况的话,你的程序退出函数,就不能调用do_exit,或者退出时发送一个信号给父进程
页: [1]
查看完整版本: 请问内核线程怎么自己退出?????????