Treviño Treviño - 1 year ago 140
Linux Question

Get number of CPUs in Linux using C

Is there an API to get the number of CPUs available in Linux?
I mean, without using /proc/cpuinfo or any other sys-node file...

I've found this implementation using sched.h:

int GetCPUCount()
cpu_set_t cs;
sched_getaffinity(0, sizeof(cs), &cs);

int count = 0;
for (int i = 0; i < 8; i++)
if (CPU_ISSET(i, &cs))
return count;

But, isn't there anything more higher level using common libraries?

Answer Source
#include <unistd.h>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download