Setting tasks CPU affinity

Gilad Ben-Yossef May 15th, 2008

Linux kernel 2.6 and latest versions of 2.4 support two system calls that allow one to limit processes to specific CPUs.

The system calls are:

#include <sched.h>
 
int sched_setaffinity(pid_t  pid, unsigned int len, unsigned long *mask);
 
int sched_getaffinity(pid_t pid, unsigned int len, unsigned long *mask);

pid is the process id of the process to assign to certain CPUs. Use ‘0′ here to denote the current process.

len is the length of the CPU bit mask, and mask is a pointer to a bit mask denoting which CPU can the process run on.

For a good discussion and a code example, look here:

http://www-128.ibm.com/developerworks/linux/library/l-affinity.html

This post originally appeared in the Codefidence Technoblog

Comments are closed.