sched_[get|set]affinity

Linux provides a couple of system calls to set the and get the CPU affinity of the current process, this are:

int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask);
 
int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);

The official description on these system calls and data structures used, can be obtain from the man page executing

$ man sched_setaffinity