mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-13 20:12:26 +00:00
45 lines
1.1 KiB
C
45 lines
1.1 KiB
C
/*
|
|
upstream; https://stackoverflow.com/questions/10490756/how-to-use-sched-getaffinity-and-sched-setaffinity-in-linux-from-c/50117787#50117787
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
|
#include <assert.h>
|
|
#include <sched.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
void print_affinity() {
|
|
cpu_set_t mask;
|
|
long nproc, i;
|
|
|
|
if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
|
|
perror("sched_getaffinity");
|
|
assert(false);
|
|
} else {
|
|
nproc = sysconf(_SC_NPROCESSORS_ONLN);
|
|
printf("sched_getaffinity = ");
|
|
for (i = 0; i < nproc; i++) {
|
|
printf("%d ", CPU_ISSET(i, &mask));
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
cpu_set_t mask;
|
|
|
|
print_affinity();
|
|
printf("sched_getcpu = %d\n", sched_getcpu());
|
|
CPU_ZERO(&mask);
|
|
CPU_SET(0, &mask);
|
|
if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
|
|
perror("sched_setaffinity");
|
|
assert(false);
|
|
}
|
|
print_affinity();
|
|
printf("sched_getcpu = %d\n", sched_getcpu());
|
|
return EXIT_SUCCESS;
|
|
}
|