I am writing a small C application that use some threads for processing data. I want to be able to know the number of processors on a certain machine, without using system() & in combination to a small script.
The only way i can think of is to parse /proc/cpuinfo. Any other useful suggestions ?
Press Ctrl + Shift + Esc to open Task Manager. Select the Performance tab to see how many cores and logical processors your PC has.
The way to tell how may cores you have is to look for "cpu cores" in your /proc/cpuinfo file. This line will show up for each virtual processor. If the number of cores shown is less than the number of virtual processors, your system is multi-threading.
Method 1: Finding the number of cores in Ubuntu using the “lscpu” command. The 'lscpu' command provides all the information related to the CPU Architecture. The above command will show all the information related to the CPU, like CPU Architecture, the number of CPU cores, threads per core, etc.
As others have mentioned in comments, this answer is useful:
numCPU = sysconf( _SC_NPROCESSORS_ONLN );
Leaving as a solution for folks that might skip over comments...
Why not use sys/sysinfo.h?
#include <sys/sysinfo.h>
#include <stdio.h>
void main () {
printf ("You have %d processors.\n", get_nprocs ());
}
Way more information can be found on the man page
$ man 3 get_nprocs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With