【发布时间】:2014-01-08 01:19:38
【问题描述】:
只有一个 kthread,我想控制它在特定的 CPU 上运行。 主进程通过 kthread_create() 和 wake_up_process() 函数创建和唤醒 kthread。 在创建kthread 时,maie 进程将kthread 的pid 存储在全局变量中。让它称为“thread_pid”。
我创建函数来改变 kthread 的 CPU。 它看起来像“int change_cpu(int cpu_to_change)”。 它在将参数 pid 作为“thread_pid”传递时使用 sched_setaffinity()。 即它调用像“sched_setaffinity(thread_pid, cpu_mask_to_change);”。 并将参数“cpu_to_change”的值存储到全局变量中。让它称为“thread_cpu”。
kthread 有断言,例如“ASSERT(smc_processor_id() == thread_cpu)”。 kthread 通常不运行而是等待完成。
我希望在调用 change_cpu() 函数后,kthread 运行良好,不会出现断言失败。 但它属于断言失败,即使 sched_setaffinity() 也能成功。 为什么它没有按预期工作? 我想知道为什么这种方式行不通。
这里是为了更好理解的虚拟代码。
int thread_cpu;
int thread_pid;
int dummy_kthread(void *args)
{
while(1) {
wait_for_completion();
ASSERT( smc_processor_id() == thread_cpu );
'''
do something
'''
complete();
}
}
int change_cpu(int cpu_to_change)
{
struct cpumask * cpu_mask;
thread_cpu = cpu_to_change;
cpu_mask = set_cpumask(cpu_to_change); // this is not actually exist function.
return sched_setaffinity(thread_pid, cpu_mask);
}
int main(){
struct task_struct *dummy;
dummy = kthread_create(dummy_kthread, NULL, "dummy_kthread");
thread_pid = get_pid(dummy); // this is not actually exist function.
}
【问题讨论】:
-
什么版本的linux?您应该使用 sched_getcpu 和 cpu_set_t。 sched_setaffinity() 也适用于进程,而不是线程。
-
linux的版本是3.4。你为什么提到sched_getcpu?我不明白。