Files
linux-kernel-module-cheat/userland/linux/getcpu_syscall.c
Ciro Santilli 六四事件 法轮功 b0d0e389e6 userland/linux/getcpu_syscall.c: fix build
2020-05-19 01:00:00 +00:00

43 lines
958 B
C

/* https://cirosantilli.com/linux-kernel-module-cheat#getcpu */
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
void* main_thread(void *arg) {
(void)arg;
unsigned cpu, numa;
assert(!syscall(SYS_getcpu, &cpu, &numa, NULL));
printf("%u %u\n", cpu, numa);
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;
}