As a demonstration, we provide a small user space C program to query and set a user space process (or thread's) CPU affinity mask. Querying the CPU affinity mask is achieved with the sched_getaffinity(2) system call and by setting it with its counterpart:
#define _GNU_SOURCE
#include <sched.h>
int sched_getaffinity(pid_t pid, size_t cpusetsize,
cpu_set_t *mask);
int sched_setaffinity(pid_t pid, size_t cpusetsize,
const cpu_set_t *mask);
A specialized data type called cpu_set_t is what is used to represent the CPU affinity bitmask; it's quite sophisticated: its size is dynamically allocated based on the number of CPU cores seen on the system. This CPU mask (of type cpu_set_t) must first be initialized to zero; the CPU_ZERO() macro achieves this (several similar helper macros exist; do refer to the man page on CPU_SET(3)). The second parameter in both...