Is there a progamatic method to set CPU affinity for a process in c/c++ for the linux operating system.
+10
A:
You need to use sched_setaffinity(2)
For example, to run on CPUs 0 and 2 only:
#include <sched.h>
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
CPU_SET(2, &mask);
result = sched_setaffinity(0, sizeof(mask), &mask);
(0
for the first parameter means the current process, supply a PID if it's some other process you want to control).
Alnitak
2008-11-11 13:52:48
+1
A:
Use sched_setaffinity at the process level, or pthread_attr_setaffinity_np for individual threads.
puetzk
2008-11-11 13:57:05
+1
A:
In short
unsigned long mask = 7; /* processors 0, 1, and 2 */
unsigned int len = sizeof(mask);
if (sched_setaffinity(0, len, &mask) < 0) {
perror("sched_setaffinity");
}
Look in CPU Affinity for more details
thAAAnos
2008-11-11 14:20:56