说我有以下c程序:
#include <stdio.h>
int main()
{
printf("Hello world \n");
getchar();
return 0;
}
gcc 1.c -o helloworld
Run Code Online (Sandbox Code Playgroud)
并说,我有一个双核机器:
cat /proc/cpuinfo | grep processor | wc -l
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,当我们执行程序时,我们如何强制该程序在core-0(或任何其他特定核心)中运行?
如何以编程方式执行此操作?例子,api,代码参考会有所帮助.
如果没有api可用,那么有没有编译时间,链接时间,加载时间这样做的方式?
OTOH,如何检查程序是在core-0还是core-1(或任何其他核心)中运行?
既然你在谈论/ proc/cpu,我假设你正在使用linux.在linux中你会使用这个sched_setaffinity功能.在你的例子中,你会打电话
cpu_set_t set;
CPU_ZERO(&set); // clear cpu mask
CPU_SET(0, &set); // set cpu 0
sched_setaffinity(0, sizeof(cpu_set_t), &set); // 0 is the calling process
Run Code Online (Sandbox Code Playgroud)
查找man sched_setaffinity更多详细信息.