-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaffinity.c
More file actions
41 lines (35 loc) · 991 Bytes
/
affinity.c
File metadata and controls
41 lines (35 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h> /* exit */
#include <unistd.h> /* sysconf */
int main(void) {
int i, nrcpus;
cpu_set_t mask;
unsigned long bitmask = 0;
// 把0号和1号1CPU加入到mask中
CPU_ZERO(&mask);
CPU_SET(0, &mask);
CPU_SET(1, &mask);
// 设置CPU亲和性
if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
perror("sched_setaffinity");
exit(EXIT_FAILURE);
}
// 获取CPU情和性
CPU_ZERO(&mask);
if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
perror("sched_getaffinity");
exit(EXIT_FAILURE);
}
// 获取逻辑CPU数量
nrcpus = sysconf(_SC_NPROCESSORS_CONF);
for (i = 0; i < nrcpus; i++) {
if (CPU_ISSET(i, &mask)) {
bitmask |= (unsigned long)0x01 << i;
printf("processor #%d is set\n", i);
}
}
printf("bitmask = %#lx\n", bitmask);
exit(EXIT_SUCCESS);
}