-
-
Notifications
You must be signed in to change notification settings - Fork 618
Expand file tree
/
Copy pathsched_getcpu.c
More file actions
42 lines (39 loc) · 940 Bytes
/
sched_getcpu.c
File metadata and controls
42 lines (39 loc) · 940 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
42
/* https://cirosantilli.com/linux-kernel-module-cheat#getcpu */
#define _GNU_SOURCE
#include <assert.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <sched.h> /* sched_getcpu */
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
void* main_thread(void *arg) {
(void)arg;
printf("%d\n", sched_getcpu());
return NULL;
}
int main(int argc, char **argv) {
pthread_t *threads;
unsigned int nthreads, i;
if (argc > 1) {
nthreads = strtoll(argv[1], NULL, 0);
} else {
nthreads = 1;
}
threads = malloc(nthreads * sizeof(*threads));
for (i = 0; i < nthreads; ++i) {
assert(pthread_create(
&threads[i],
NULL,
main_thread,
NULL
) == 0);
}
for (i = 0; i < nthreads; ++i) {
pthread_join(threads[i], NULL);
}
free(threads);
return EXIT_SUCCESS;
}