5
如果我有下面的内核线程功能:如果我们从内核线程返回,是否需要使用kthread_stop?
int thread_fn() {
printk(KERN_INFO "In thread1");
return 0;
}
我还需要在这里使用kthread_stop()
功能?
请问return
中的线程函数是否让内核线程停止并退出?
如果我有下面的内核线程功能:如果我们从内核线程返回,是否需要使用kthread_stop?
int thread_fn() {
printk(KERN_INFO "In thread1");
return 0;
}
我还需要在这里使用kthread_stop()
功能?
请问return
中的线程函数是否让内核线程停止并退出?
如果你看kthread()
是implemented如何在第209行调用threadfn(data)
并在ret
中存储退出码;那么它叫do_exit(ret)
。
所以从您的threadfn
简单返回就足够了。
如果你看看kthread_stop的文档,它说,它:
kthread_should_stop
返回true;这意味着kthread_stop()
只应从线程外部调用来停止线程。由于它等待线程完成,所以不能在线程内调用它,否则可能会导致死锁!
此外,文档说它只通知线程它应该退出,线程应该调用kthread_should_stop
来了解这个。因此,一个长寿命threadfn
可以这样做:
int thread_fn() {
printk(KERN_INFO "In thread1");
while (!kthread_should_stop()) {
get_some_work_to_do_or_block();
if (have_work_to_do())
do_work();
}
return 0;
}
但是,如果你的函数不长寿命,呼吁kthread_should_stop
是没有必要的。
我想你应该可以通过查看启动线程的代码来查看它是否能够设置线程的栈,并且返回的地址会在某个地方显示。 –