设置cpu亲和性---即 绑定特定的进程线程到指定的cpu
#include <unistd.h> #include <pthread.h> #include <stdio.h> int main(int argc, char * argv[]) { //获取当前进程的 pid pid_t pid = getpid(); //创建 cpu 集合 cpu_set_t mask; //初始化 cpu 集合 CPU_ZERO(&mask); //将 cpu 1 加入 集合 CPU_SET(1, &mask); //设置 进程的cpu亲和性:sched_setaffinity(pid, sizeof (mask), &mask),如果 pid 为 0,則表示当前进程 if (sched_setaffinity(pid, sizeof (mask), &mask) == -1) { printf("WARNING: Could not set CPU Affinity, continuing...\n"); } }
注: