-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpu_affinity_thread.c
More file actions
64 lines (54 loc) · 1.64 KB
/
cpu_affinity_thread.c
File metadata and controls
64 lines (54 loc) · 1.64 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#define _GNU_SOURCE
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <pthread.h>
double waste_time(long n){
double res = 0;
long i = 0;
while(i < n * 200000){
i++;
res += sqrt(i);
}
return res;
}
void *thread_func(void *param){
unsigned long mask = 0b0001; /* process 0 */
/* bind process to processor 0 */
if(pthread_setaffinity_np(pthread_self(), sizeof(mask), (cpu_set_t *)&mask) < 0){
perror("pthread_setaffinity_np");
}
/* waste some time so the work is visible with "top" */
printf("result: %f\n", waste_time(2000));
mask = 0b0010;
sleep(2);
if(pthread_setaffinity_np(pthread_self(), sizeof(mask), (cpu_set_t *)&mask)){
perror("pthread_setaffinity_np");
}
/* waste some more time to see the processor switch */
printf("result: %f\n", waste_time(2000));
mask = 0b0100;
sleep(2);
if(pthread_setaffinity_np(pthread_self(), sizeof(mask), (cpu_set_t *)&mask)){
perror("pthread_setaffinity_np");
}
/* waste some more time to see the processor switch */
printf("result: %f\n", waste_time(2000));
mask = 0b1000;
sleep(2);
if(pthread_setaffinity_np(pthread_self(), sizeof(mask), (cpu_set_t *)&mask)){
perror("pthread_setaffinity_np");
}
/* waste some more time to see the processor switch */
printf("result: %f\n", waste_time(2000));
return 0;
}
int main(int argc, char * argv[]){
pthread_t my_thread;
if(pthread_create(&my_thread, NULL, thread_func, NULL) != 0){
perror("pthread_create");
}
// pthread_exit(NULL);
pthread_join(my_thread, NULL);
return 0;
}