I was thinking about getting physical CPUs in Linux using C. I know, I can simply do this:
if(!(cpus = popen("cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
", "r")))
{
// ...
}
but that's not the point, it won't work when I don't have grep
installed. I came up with another idea:
- simply parse
/proc/cpuinfo
count number of physical ids:
if(sscanf(buff, "physical id : %d", &physicalID) == 1) i++;
save them into an array:
if(sscanf(buff, "physical id : %d", &physicalID) == 1) { ids[i] = physicalID; i ++; }
check if there are different numbers in the array, if so, count them = it will give me number of physical sockets (CPUs), right?
And I did it but I'm not quite sure if it will be always true ... And how about couting cores, logical CPUs and checking if hyperthreading is enabled? How can I do this with /proc/cpuinfo
?