How it works...
Step 1 does two things. First, it prints the default affinity mask. We can see that the process is scheduled to run on all the processors. Second, it prepares cpu_set_t, which represents a set of CPUs, by initializing it with the CPU_ZERO macro and setting the affinity on CPU 3 with the CPU_SET macro. Note that the cpu_set_t object must be manipulated directly but only via the macro provided. A full list of macros is documented on the man page: man cpu_set.
Step 2 calls the sched_setaffinity() system call to set the affinity (specified in the mask variable, that is, cpu_set_t) on the process with the PID returned by the getpid() function. We could have passed 0 instead of getpid(), meaning the current process. After the
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access