How to use sched_getaffinity and sched_setaffinity in Linux from C?

affinity, c, linux, process, schedule

Solution

To use sched_setaffinity to make the current process run on core 7 you do this:

cpu_set_t my_set;        /* Define your cpu_set bit mask. */
CPU_ZERO(&my_set);       /* Initialize it all to 0, i.e. no CPUs selected. */
CPU_SET(7, &my_set);     /* set the bit that represents core 7. */
sched_setaffinity(0, sizeof(cpu_set_t), &my_set); /* Set affinity of tihs process to */
                                                  /* the defined mask, i.e. only 7. */

See http://linux.die.net/man/2/sched_setaffinity & http://www.gnu.org/software/libc/manual/html_node/CPU-Affinity.html for more info.

Problem

I am trying to: Run 16 copies concurrently with processor pinning (2 copies per core) Run 8 copies concurrently with processor pinning (2 copies per core) and flipping processor core to the furthest core after certain function say function 1 finishes. The problem I am facing is how to select the farthest processor. Some friends suggested to use sched_getaffinity and sched_setaffinity but I count not find any good examples.

Original source

Related problems