如何在Linux中从C使用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.
这篇关于如何在Linux中从C使用sched_getaffinity和sched_setaffinity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!