How to get CPU information in C on Linux, for example, the number of cores?

Is it possible to get such information with some API or function, rather than parsing /proc/cpuinfo ?

+6
source share
9 answers

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.

+7
source

Read /proc/cpuinfo

Output result

 processor : 0 model name : Intel(R) Xeon(R) CPU E5410 @ 2.33GHz cache size : 6144 KB physical id : 0 siblings : 4 core id : 0 cpu cores : 4 processor : 1 model name : Intel(R) Xeon(R) CPU E5410 @ 2.33GHz cache size : 6144 KB physical id : 0 siblings : 4 core id : 1 cpu cores : 4 processor : 2 model name : Intel(R) Xeon(R) CPU E5410 @ 2.33GHz cache size : 6144 KB physical id : 0 siblings : 4 core id : 2 cpu cores : 4 processor : 3 model name : Intel(R) Xeon(R) CPU E5410 @ 2.33GHz cache size : 6144 KB physical id : 0 siblings : 4 core id : 3 cpu cores : 4 

show_cpuinfo is a function that actually implements /proc/cpuinfo functionality

+4
source

You can use this for most linux distro types

For code C

  num_cpus = sysconf( _SC_NPROCESSORS_ONLN ); 

(On QNX systems, you can use num_cpus = sysinfo_numcpu() )

For shell scripts you can use cat /proc/cpuinfo

or use lscpu or nproc in linux

+4
source

libcpuid provides a simple API that will directly return all CPU functions, including the number of cores. To get the number of cores at runtime, you can do something like this:

 #include <stdio.h> #include <libcpuid.h> int main(void) { if (!cpuid_present()) { printf("Sorry, your CPU doesn't support CPUID!\n"); return -1; } struct cpu_raw_data_t raw; struct cpu_id_t data; if (cpuid_get_raw_data(&raw) < 0) { printf("Sorry, cannot get the CPUID raw data.\n"); printf("Error: %s\n", cpuid_error()); return -2; } if (cpu_identify(&raw, &data) < 0) { printf("Sorrry, CPU identification failed.\n"); printf("Error: %s\n", cpuid_error()); return -3; } printf("Processor has %d physical cores\n", data.num_cores); return 0; } 
+3
source

Parse the / proc / cpuinfo file. This will give you a lot of details about the processor. Extract the appropriate fields into your C / C ++ file.

+1
source

Add the following line to the source code.

 system("cat /proc/cpuinfo | grep processor | wc -l"); 

This will print the number of processors in your system. And if you want to use this output of this system call in your program, than use the popen system call.

+1
source

No, it is not. Either you should analyze the cpuinfo file, or some library will do it for you.

+1
source

Depending on your taste for Linux, you will get different results from / proc / cpuid.

This works for me on CentOS to get the total number of cores.

 cat /proc/cpuinfo | grep -w cores | sed -e 's/\t//g' | awk '{print $3}' | xargs | sed -e 's/\ /+/g' | bc 

The same does not work in Ubuntu. For Ubuntu, you can use the following command.

 nproc 
+1
source

Have you ever seen the output of this shell command "cat / proc / cpuinfo"? I think you can get all the necessary information. To read the information in a C program, I would prefer file manipulation functions like fopen, fgets, etc.

0
source

Source: https://habr.com/ru/post/910319/


All Articles