0
我做了一些在Linux KTHREAD功能測試,並具有以下模塊:Linux內核 - kthread_stop總是返回-EINTR?
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/smp.h>
#include <linux/cpumask.h>
#include <linux/err.h>
MODULE_LICENSE("GPL");
int function(void *data)
{
printk(KERN_DEBUG "CPU: %u\n", smp_processor_id());
return 0;
}
/* Function executed upon loading driver */
int __init init_module(void)
{
unsigned int cpu;
printk(KERN_DEBUG "init\n");
for_each_present_cpu(cpu)
{
struct task_struct *thread = kthread_create(function, NULL, "Thread %u", cpu);
if (!IS_ERR(thread))
{
int result;
/* Bind the thread to the current core */
kthread_bind(thread, cpu);
/* Start the thread */
wake_up_process(thread);
/* Wait for the thread function to complete */
result = kthread_stop(thread);
printk(KERN_DEBUG "Thread %u stopped with result %d\n", cpu, result);
}
else
{
printk(KERN_ALERT "Could not create a thread bound to core number %u\n", cpu);
break;
}
}
return 0;
}
/* Function executed when unloading module */
void cleanup_module(void)
{
printk(KERN_DEBUG "exit\n");
}
從我的理解,我應該能夠看到所有的「CPU:...」打印在init_module結束之前。但是,kthread_stop始終返回-EINTR,就好像wake_up_process實際上未被調用一樣。我究竟做錯了什麼?
有意義。但是,kthread_stop上的註釋說:「將@k的kthread_should_stop()設置爲返回true,將其喚醒並等待它退出」。 「等待它退出」部分使得聽起來像調用者線程放棄CPU。 – Martin
cmon男人。在那種情況下,當孩子第一次運行標誌已經設置並且你的例程永遠不會被調用時,可能是這樣。你有代碼,你可以輕鬆地添加printks來檢查。請參閱kthread。那裏有2個EINTR。 –
兩個EINTR在哪裏?無論如何,如果你所說的話是真的,那麼這個評論最好是誤導性的。它的措辭使得它聽起來像是和pthread_join類似。在名爲「kthread」的函數中使用 – Martin