clinuxprocessscheduleaffinity

How to use sched_getaffinity and sched_setaffinity in Linux from C?


I am trying to:

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.


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.