-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsched_setaffinity.c
73 lines (50 loc) · 1.52 KB
/
sched_setaffinity.c
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
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <math.h> // -lm
#include <sched.h>
#include <pthread.h> //lpthread
double waste_time(long n)
{
double res = 0;
long double i = 0;
for(i=0; i <n * 200000; i++)
res += sqrt(i);
return res;
}
void *work1(void *arg)
{
unsigned long mask = 1; /* processor 0 */
/* bind process to processor 0 */
if (sched_setaffinity(0, sizeof(mask), &mask) <0) { // int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
perror("sched_setaffinity error\n");
}
/* waste some time so the work is visible with "top" */
printf ("result: %f\n", waste_time (20000));
return 0;
}
void *work2(void *arg)
{
unsigned long mask = 2; /* process switches to processor 1 now */
if (sched_setaffinity(0, sizeof(mask), &mask) <0) {
perror("sched_setaffinity error\n");
}
/* waste some more time to see the processor switch */
printf ("result: %f\n", waste_time (20000));
return 0;
}
/*
int
pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg);
分别绑定不同CPU核心
*/
int main(int argc, char **argv)
{
int err = 0;
pthread_t t_id1, t_id2;
if( pthread_create (&t_id1, NULL, &work1, NULL) != 0)
printf("Error err = %d\n", err);
if(pthread_create (&t_id2, NULL, &work2, NULL) != 0)
printf("Error err = %d\n", err);
pthread_join(t_id1, NULL);
pthread_join(t_id2, NULL);
return 0;
}