Are you intending for only the
parent child process to have CPU affinity?
if(sched_setaffinity(0, sizeof(mask), &mask) == -1) {
perror("sched_setaffinity()");
return EXIT_FAILURE;
}
If not you should probably change the if statement to:
if(sched_setaffinity(getpid(), sizeof(mask), &mask) == -1) {
perror("sched_setaffinity()");
return EXIT_FAILURE;
}
I think this is what the example in the PDF intended (lock both processes to the same CPU core)
"One difficulty in measuring context-switch cost arises in systems
with more than one CPU; what you need to do on such a system is
ensure that your context-switching processes are located on the
same processor. Fortunately, most operating systems have calls to
bind a process to a particular processor; on Linux, for example, the
sched_setaffinity() call is what you’re looking for. By ensuring both
processes are on the same processor, you are making sure to measure
the cost of the OS stopping one process and restoring another on
the same CPU."