0

可能重复:
CPU 关联性

在运行 Linux (fc12) 的 x86 机器上,我可以让进程在多处理器系统的特定内核上运行吗?我知道有一个功能sched_setaffinity可以帮助选择处理器,但我想在处理器的特定核心上执行该过程。

4

2 回答 2

1

不确定这是否是您需要的,我使用此代码使线程在特定核心上运行。使用 -pthread 编译和链接。

#include "pthread.h"
#include "sched.h"

int affinity = 3; //core id
pthread_t mythread;
mythread = pthread_self();
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(affinity, &cpuset); //lets kernel know only core affinity may run this thread
if (pthread_setaffinity_np(mythread, sizeof(cpu_set_t), &cpuset) <0){
    perror("sched_set_affinity");
}
于 2012-08-30T12:56:28.990 回答
0

AFAIK 你无法区分 CPU 内核和 CPU。就 Linux 而言,核心是成熟的 CPU。例如,如果您有 2 个 CPU,每个 CPU 有 2 个内核,Linux 会认为您有 4 个 CPU。

于 2012-08-30T18:42:12.273 回答