mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 18:25:57 +01:00
br2: enable taskset, create sched_getaffinity, yet undocumented
This commit is contained in:
@@ -12,6 +12,7 @@ These programs can also be compiled and used on host.
|
||||
.. link:hello.c[]
|
||||
.. link:myinsmod.c[]
|
||||
.. link:myrmmod.c[]
|
||||
.. link:sched_getaffinity.c[]
|
||||
.. link:usermem.c[]
|
||||
... link:pagemap_dump.c[]
|
||||
.. inits
|
||||
|
||||
44
kernel_module/user/sched_getaffinity.c
Normal file
44
kernel_module/user/sched_getaffinity.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user