Files
linux-kernel-module-cheat/userland/sched_getaffinity_threads.c
Ciro Santilli 六四事件 法轮功 ca231b82f6 get rid of lkmc package, move userland and kernel-modules to top
Rationale: we already had a non buildroot build system,
maintaining both will be hard, and having short paths is more awesome.
2018-10-25 00:00:02 +00:00

52 lines
1.1 KiB
C

/* https://github.com/cirosantilli/linux-kernel-module-cheat#gdb-step-debug-multicore */
#define _GNU_SOURCE
#include <assert.h>
#include <pthread.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void* main_thread_0(void *arg) {
int i;
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(*((int*)arg), &mask);
sched_setaffinity(0, sizeof(cpu_set_t), &mask);
i = 0;
while (true) {
printf("0 %d\n", i);
sleep(1);
i++;
}
return NULL;
}
void* main_thread_1(void *arg) {
int i;
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(*((int*)arg), &mask);
sched_setaffinity(1, sizeof(cpu_set_t), &mask);
i = 0;
while (true) {
printf("1 %d\n", i);
sleep(1);
i++;
}
return NULL;
}
int main(void) {
enum NUM_THREADS {NUM_THREADS = 2};
pthread_t threads[NUM_THREADS];
int thread_args[NUM_THREADS];
pthread_create(&threads[0], NULL, main_thread_0, (void*)&thread_args[0]);
pthread_create(&threads[1], NULL, main_thread_1, (void*)&thread_args[1]);
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
return EXIT_SUCCESS;
}