LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2011, 06:50 AM   #1
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Rep: Reputation: 3
Question How to get the Number of Cores using C


Hi people!

Anybody knows How can I get the number of cores of a machine?

I know that I can use POPEN and get the stream of this:
Code:
grep -i 'processor' /proc/cpuinfo | wc -l
But, I think that a simple method to do this.

Can anybody say to me??

Thanks!!
 
Old 04-13-2011, 08:16 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You could also just count the number of directories in /sys/devices/system/cpu/.

EDIT: well, the number of directories named cpu* in /sys/devices/system/cpu/.

Last edited by dugan; 04-13-2011 at 08:35 AM.
 
Old 04-13-2011, 08:31 AM   #3
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Original Poster
Rep: Reputation: 3
Thanks dugan!

But, with this ideia I will need a POPEN again:

ls /sys/devices/system/cpu/ | grep [0-9] | wc -l

There isn't a C header to handle this question?

Thanks!!
 
Old 04-13-2011, 08:37 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You don't need a popen to list subdirectories, open files, or count lines.

http://www.gnu.org/s/libc/manual/htm...stem-Interface

Last edited by dugan; 04-13-2011 at 08:43 AM.
 
Old 04-13-2011, 09:56 AM   #5
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Original Poster
Rep: Reputation: 3
So, I will make a function who receives a string with the path of the Folder, and list this to me...

But, I think my method of POPEN its more easy for handle than this opendir and dirent...


Dugan, thanks for this help!
 
Old 04-13-2011, 10:03 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by MarcosPauloBR View Post
But, I think my method of POPEN its more easy for handle than this opendir and dirent...
What does "it's more easy for handle" mean?

Also, in case you mean you would rather use popen(), don't. It's really a bad idea.
 
Old 04-13-2011, 10:16 AM   #7
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Original Poster
Rep: Reputation: 3
MTK358,

On my OPEPN I do a ls in the folder that I need.

With this stream I make a fgets, and while has a entry I call a function to work on this file.

On this case, I think its more "directly" to take one-by-one the names of the files, and I don't need to create any pointer and don't create any Struct.

Because of this I say "its more easy for handle" =P

I think when I begin to make a ls with the libc method, I will change my mind.

=D
 
Old 04-13-2011, 10:21 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
It's just that it doesn't seem like a good idea on relying on the system having a certain shell or utilities installed. And ls itself is written in C, and uses opendir(), etc.
 
Old 04-13-2011, 10:25 AM   #9
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Original Poster
Rep: Reputation: 3
So, you change my mind =D

I agree with your idea of rely on the host tools!

As I said, I will change my code and make a function to list whatever folder that I need!

Thanks for your help MTK358!
 
Old 04-13-2011, 04:14 PM   #10
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165
Blog Entries: 1

Rep: Reputation: 22
Well I think doing it in C is pretty.

Code:
numofcpus = sysconf(_SC_NPROCESSORS_ONLN); // Get the number of logical CPUs.
You can use sysconf() to get all kinds of configuration stuff.
http://www.gnu.org/s/libc/manual/htm...ts-for-Sysconf
 
1 members found this post helpful.
Old 04-14-2011, 09:54 AM   #11
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Original Poster
Rep: Reputation: 3
yaplej, its this exactly what I want!

I knew there is a simple method to get the number of cores on C!

Thanks for your help!
 
  


Reply



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
[SOLVED] Need help in identifying the number of cores in each processor running srajeshkumar Linux - Server 7 12-23-2010 08:52 AM
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
missing number of cores in according to /proc/cpuinfo centguy Linux - Software 14 01-10-2010 09:22 PM
Find number of cores in my processor ttsdinesh Linux - Newbie 8 12-02-2009 06:16 AM
how to find number of cores in CPU narensr Linux - Hardware 5 08-24-2006 01:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 AM.

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
Open Source Consulting | Domain Registration