gpt4 book ai didi

linux - 在 Linux 内核中使用 "sched_setaffinity()"

转载 作者:太空狗 更新时间:2023-10-29 12:09:03 27 4
gpt4 key购买 nike

很多关于 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 吗?

最佳答案

在挖掘内核文件寻找 cpumask 出现的位置后,我自己找到了答案。

你可以使用这两个函数:

cpumask_clear(struct cpumask *dstp)//清除你即将使用的掩码

cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)//设置cpu数量

所以我用的是这样的:

struct cpumask mask;  
cpumask_clear(&mask);
cpumask_set_cpu(cpuNumber, &mask);
sched_setaffinity(pid, &mask);

关于linux - 在 Linux 内核中使用 "sched_setaffinity()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55013836/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com