LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 10-26-2009, 02:04 PM   #1
gamalpha
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, 02:20 PM   #2
paulsm4
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, 03:23 PM   #3
tsandholm
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
#
 
  


Reply


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 01:31 AM
Interpreting lshw output and CPU count vs hyperthreading kkoene Linux - Newbie 1 05-31-2009 05:59 PM
Howto reduce CPU count bret Linux - General 1 09-26-2007 06:12 PM
I want to count CPU cycles used by arbitrary code JonBrant Programming 4 11-10-2006 04:43 PM
%CPU , Memory Usage, Thread count srinivasar Programming 0 09-20-2003 03:56 AM


All times are GMT -5. The time now is 12:39 PM.

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