Akin to how (in the preceding chapter) we used the convenient user space utility program, chrt(1) to get (or set) a process' (or thread's) scheduling policy and/or priority, you can use the user space taskset(1) utility to get and/or set a given process' (or thread's) CPU affinity mask. A couple of quick examples follow; note that these examples were run on an x86_64 Linux system with 4 CPU cores:
- Use taskset to query the CPU affinity mask of systemd (PID 1):
$ taskset -p 1
pid 1's current affinity mask: f
$
- Use taskset to ensure that the compiler – and its descendants (the assembler and linker) – run only on the first two CPU cores; the first parameter to taskset is the CPU affinity bitmask (03 is binary 0011):
$ taskset 03 gcc userspc_cpuaffinity.c -o userspc_cpuaffinity -Wall
Do look up the man page on taskset(1) for complete usage details.