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 an example of code that reads and prints information to a console stolen from forums . This is really just a specialized cat
team.
#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 analyze and compare the physical id
, core id
and cpu cores
to get an accurate result if you really care about the number of processors and processor cores. Also note that if there is htt
in flags
, you are using a processor with hyperthreads, which means that your mileage may vary.
Also note that if you run the kernel in a virtual machine, you will only see CPU cores designed for the VM guest system.
source share