在 Linux 内核中使用“sched_setaffinity()”

2024-01-01

网上有很多帖子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);

但我对如何正确创建感到困惑new_mask具有正确 CPU 编号的参数。关于它的文档不是很有帮助。

有人可以展示如何在内核空间中使用此函数将进程设置为特定 CPU 的示例吗?


在深入挖掘内核文件寻找位置后,我自己找到了答案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); 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Linux 内核中使用“sched_setaffinity()” 的相关文章

随机推荐