如何从 C 在 Linux 中使用 sched_getaffinity 和 sched_setaffinity? [英] How to use sched_getaffinity and sched_setaffinity in Linux from C?
问题描述
我正在尝试:
与处理器固定同时运行 16 个副本(每个内核 2 个副本)
Run 16 copies concurrently with processor pinning (2 copies per core)
与处理器固定同时运行 8 个副本(每个内核 2 个副本),并在某个函数说函数 1 完成后将处理器内核翻转到最远的内核.
Run 8 copies concurrently with processor pinning (2 copies per core) and flipping processor core to the furthest core after certain function say function 1 finishes.
我面临的问题是如何选择最远的处理器.
The problem I am facing is how to select the farthest processor.
一些朋友建议使用sched_getaffinity和sched_setaffinity,但我数了数没有找到任何好的例子.
Some friends suggested to use sched_getaffinity and sched_setaffinity but I count not find any good examples.
推荐答案
要使用 sched_setaffinity 使当前进程在核心 7 上运行,请执行以下操作:
To use sched_setaffinity to make the current process run on core 7 you do this:
cpu_set_t my_set; /* Define your cpu_set bit mask. */
CPU_ZERO(&my_set); /* Initialize it all to 0, i.e. no CPUs selected. */
CPU_SET(7, &my_set); /* set the bit that represents core 7. */
sched_setaffinity(0, sizeof(cpu_set_t), &my_set); /* Set affinity of tihs process to */
/* the defined mask, i.e. only 7. */
参见http://linux.die.net/man/2/sched_setaffinity&http://www.gnu.org/software/libc/manual/html_node/CPU-Affinity.html 了解更多信息.
See http://linux.die.net/man/2/sched_setaffinity & http://www.gnu.org/software/libc/manual/html_node/CPU-Affinity.html for more info.
这篇关于如何从 C 在 Linux 中使用 sched_getaffinity 和 sched_setaffinity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!