Frank
Frank

Reputation: 1414

Writing a C program to only run on a single processor core

How can I force an application I'm writing to only use a single processor core?

Upvotes: 1

Views: 240

Answers (1)

Gearoid Murphy
Gearoid Murphy

Reputation: 12116

There is a Linux system call specifically for this purpose called sched_setaffinity

For example, to run on CPU 0:

#include <sched.h>
int main(void)
{
    cpu_set_t  mask;
    CPU_ZERO(&mask);
    CPU_SET(0, &mask);
    result = sched_setaffinity(0, sizeof(mask), &mask);
    return 0;
}

Upvotes: 7

Related Questions