views:

328

answers:

4

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 ?

+1  A: 
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.

WhirlWind
+2  A: 

see man sysconf()

frankc
+1  A: 

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

Vineel Kumar Reddy