LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need cpu count in linux (https://www.linuxquestions.org/questions/linux-newbie-8/need-cpu-count-in-linux-764606/)

gamalpha 10-26-2009 02:04 PM

Need cpu count in linux
 
I would like my c++ program to get the cpu count of an intel processor with the linux operating system. I have found code on the internet but they all uses window include files and won't run under linux. I read that one can get the cpu count by typing grep -c processor /proc/cpuinfo in a linux shell but I need it in my program. Would I have to make a system call, save it to a file and then read it in?

paulsm4 10-26-2009 02:20 PM

Hi -

"/proc/cpuinfo" is exactly what I was going to suggest. Exactly right!

You can easily read this info from C/C++ using:

a) "fopen/fgets" (of the /proc/cpuinfo files), or using

b) "popen()/fgets" (of the "grep /proc/cpuinfo" command).

'Hope that helps .. PSM

tsandholm 10-26-2009 03:23 PM

But I like this even better:

# ls /sys/devices/system/cpu
cpu0 cpu1 cpu2 cpu3
#


All times are GMT -5. The time now is 03:24 PM.