CPU limit in C

I tested c code on a physical and virtual machine and I need to limit the number. processor used during program execution c. Is there any way to do this?

+5
source share
2 answers

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

+6
source

-, Windows SetProcessAffinityMask:

SetProcessAffinityMask(GetCurrentProcess(), 0x1); //Only CPU #1
+6

All Articles