#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define __USE_GNU
#include <sched.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
void process_affinity(int num) {
//gettid();
pid_t selfid = syscall(__NR_gettid);
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(1, &mask);
//selfid
sched_setaffinity(0, sizeof(mask), &mask);
while(1) ;
}
int main() {
// 8
int num = sysconf(_SC_NPROCESSORS_CONF);
int i = 0;
pid_t pid = 0;
for (i = 0;i < num/2;i ++) {
pid = fork();
if (pid <= (pid_t)0) {
break;
}
}
if (pid == 0) {
process_affinity(num);
}
while (1) usleep(1);
return 0;
}
cpu绑定进程
最新推荐文章于 2024-05-21 19:41:47 发布