Wednesday, April 12, 2006

Bind a thread to a particular processor

Use the linux system call, sched_setaffinity to bind a thread to a particular processor.
For example
the following code will bind the main thread/process to cpu No. 0


#define _GNU_SOURCE
#include

void run_on_cpu(int cpu)
{
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu,&mask);
sched_setaffinity(0,sizeof(cpu_set_t),&mask);
}

main()
{
run_on_cpu(0);
while(1);
}

No comments: