sched_setaffinity
上有很多帖子,但是在内核空间中使用此帖子几乎没有。
我正在使用内核4.14.79。
我尝试使用用户空间方法以以下形式调用sched_setaffinity
:
cpu_set_t my_set;
CPU_ZERO(&my_set);
CPU_SET(7, &my_set);
sched_setaffinity(0, sizeof(cpu_set_t), &my_set);
但是在尝试编译内核时,我遇到了错误,并且意识到这种形式在内核空间中不起作用。
我看到sched_affinity
是在sched.h
中定义的,其格式为:
extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
但是我对如何使用正确的CPU编号正确创建new_mask
参数感到困惑。关于它的文档不是很有帮助。
有人可以举例说明如何在内核空间中使用此功能来将进程设置为特定的CPU吗?
答案 0 :(得分:0)
在浏览内核文件以寻找cpumask
出现的位置后,自己找到了答案。
您可以使用以下两个功能:
cpumask_clear(struct cpumask *dstp) //clear the mask you are about to use
cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp) //set the cpu number
所以我用的是这样的:
struct cpumask mask;
cpumask_clear(&mask);
cpumask_set_cpu(cpuNumber, &mask);
sched_setaffinity(pid, &mask);