Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-26-2009, 03:04 PM   #1
LQ Newbie
Registered: Oct 2009
Posts: 1

Rep: Reputation: 0
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?
Old 10-26-2009, 03:20 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
Old 10-26-2009, 04:23 PM   #3
LQ Newbie
Registered: Oct 2009
Location: New Hampshire
Distribution: debian/opensuse
Posts: 10

Rep: Reputation: 3
But I like this even better:

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
CPU core count raakin Linux - Newbie 14 08-01-2014 02:31 AM
Interpreting lshw output and CPU count vs hyperthreading kkoene Linux - Newbie 1 05-31-2009 06:59 PM
Howto reduce CPU count bret Linux - General 1 09-26-2007 07:12 PM
I want to count CPU cycles used by arbitrary code JonBrant Programming 4 11-10-2006 05:43 PM
%CPU , Memory Usage, Thread count srinivasar Programming 0 09-20-2003 04:56 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:53 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration