Search code examples
linuxcpuaffinity

Change default CPU affinity


I would like to know whether it's possible the default affinity for linux processes. The default value is ~0 (truncated to the number of CPUs available) but I'd like to be able to set it for all the process of the system. It would be also nice to do this at boot time so I could effectively prevent any process from using certain CPUs (unless explicitely set by a syscall).

Thanks! David


Solution

  • From C program:

    #define _GNU_SOURCE
    #include <sched.h>
    
    int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
    

    see man sched_setaffinity for further information.

    From the shell:

    taskset <mask> <command> <args>
    

    or

    taskset -p <pid> <mask>
    

    where <mask> is, for example, 0x00000001 for the first CPU.