How do I retrieve the number of processors on C / Linux?
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 ?
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
machine:/sys/devices/system/cpu$ ls
cpu0 cpu3 cpu6 kernel_max perf_counters sched_mc_power_savings
cpu1 cpu4 cpu7 offline possible
cpu2 cpu5 cpuidle online present
If you have a machine with sysfs, take a look in /sys/devices/system/cpu.
Make sure you're asking for what you want -- CPUs, cores, hyperthreads, etc.
The following was the code that I used to figure number of cores.....it might help you
//Finding the number of cores(logical processor) using cpuid instruction.....
__asm
{
mov eax,01h //01h is for getting number of cores present in the processor
cpuid
mov t,ebx
}
(t>>16)&0xff contains the number cores........
I guess this could help you http://lists.gnu.org/archive/html/autoconf/2002-08/msg00126.html
#include <stdio.h>
void getPSN(char *PSN)
{int varEAX, varEBX, varECX, varEDX;
char str[9];
//%eax=1 gives most significant 32 bits in eax
__asm__ __volatile__ ("cpuid": "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (1));
sprintf(str, "%08X", varEAX); //i.e. XXXX-XXXX-xxxx-xxxx-xxxx-xxxx
sprintf(PSN, "%C%C%C%C-%C%C%C%C", str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
//%eax=3 gives least significant 64 bits in edx and ecx [if PN is enabled]
__asm__ __volatile__ ("cpuid": "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (3));
sprintf(str, "%08X", varEDX); //i.e. xxxx-xxxx-XXXX-XXXX-xxxx-xxxx
sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
sprintf(str, "%08X", varECX); //i.e. xxxx-xxxx-xxxx-xxxx-XXXX-XXXX
sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
}
int main()
{
char PSN[30]; //24 Hex digits, 5 '-' separators, and a '\0'
getPSN(PSN);
printf("%s\n", PSN); //compare with: lshw | grep serial:
return 0;
}
Here's a minimal example of how to get physical cores and virtual threads:
#include <stdio.h>
...
unsigned int thread_count, core_count;
FILE *cpu_info = fopen("/proc/cpuinfo", "r");
while (!fscanf(cpu_info, "siblings\t: %u", &thread_count))
fscanf(cpu_info, "%*[^s]");
while (!fscanf(cpu_info, "cpu cores\t: %u", &core_count))
fscanf(cpu_info, "%*[^c]");
fclose(cpu_info);
It's more portable than _SC_NPROCESSORS_ONLN
as it doesn't require glibc extensions.
Note that you don't need to check for EOF
in this example as fscanf
will return EOF
if reached. This will cause the loop to stop safely.
Also, this example doesn't contain error checking to see if fopen
failed. This should be done however you see fit.
This fscanf
technique was derived from here: https://stackoverflow.com/a/43483850
精彩评论