For Linux exists sched_setaffinity. For example, if you want it to run only on processors 1 and 3:
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(1, &set);
CPU_SET(3, &set);
sched_setaffinity(pid, CPU_SETSIZE, &set);
Caution: sched_setaffinityand sched_getaffinitydepend on Linux (they do not exist on other POSIX systems).
BSD has similar semantics. I expect Solaris to have a similar feature. cpuset_setaffinity
source
share