From man 5 proc
:
/proc/cpuinfo
This is a collection of CPU and system architecture dependent
items, for each supported architecture a different list. Two
common entries are processor which gives CPU number and
bogomips; a system constant that is calculated during kernel
initialization. SMP machines have information for each CPU.
Here is sample code that reads and prints the info to console, stolen from forums - It really is just a specialized cat
command.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *cpuinfo = fopen("/proc/cpuinfo", "rb");
char *arg = 0;
size_t size = 0;
while(getdelim(&arg, &size, 0, cpuinfo) != -1)
{
puts(arg);
}
free(arg);
fclose(cpuinfo);
return 0;
}
Please note that you need to parse and compare the physical id
, core id
and cpu cores
to get an accurate result, if you really care about the number of CPUs vs. CPU cores. Also please note that if there is a htt
in flags
, you are running a hyper-threading CPU, which means that your mileage may vary.
Please also note that if you run your kernel in a virtual machine, you only see the CPU cores dedicated to the VM guest.