LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-19-2005, 07:56 PM   #1
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Rep: Reputation: 30
Getting the number of processors


Is there any "standard" function on Linux/Unix systems to get the number of processors on the system? If not, is there any command line command that would do the same? If so, I suppose I could just call it and parse the output. I want to write a multithreaded program that only spawns as many threads as there are processors.

-Brandon R
 
Old 11-19-2005, 08:07 PM   #2
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Original Poster
Rep: Reputation: 30
Re: Getting the number of processors

Quote:
Is there any "standard" function on Linux/Unix systems to get the number of processors on the system?
Oh, and I am programming in C/C++ by the way.
 
Old 11-19-2005, 11:27 PM   #3
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
I am sure their is probably a better way of doing this. the command 'ps -A' will list all processes. you could use a simple bash script to parse out the data, or this python script
Code:
#!/usr/bin/env python
import commands
ps_output = commands.getoutput('ps -A')
print len(ps_output.splitlines()) -1
 
Old 11-19-2005, 11:39 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
cat /proc/cpuinfo | grep "^processor"|wc -l

Cheers,
Tink
 
Old 11-20-2005, 11:06 AM   #5
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Original Poster
Rep: Reputation: 30
Actually, shanenin, I was looking for a way to get the number of processors, not processes. Thank you for your response though. Good use for Python by the way. I use Python at work for generating test data and as much as I don't like the idea of languages that are interpretted and have implicitly declared variables, I always find myself wanting to type myString.split('\t') in C/C++.

I suppose I am a little disappointed there isn't a standard "int numProcessors(void)" type of solution in the POSIX libraries but Tinkster's response does just fine. Thanks.

-Brandon

Last edited by spaaarky21; 11-20-2005 at 11:22 AM.
 
Old 11-20-2005, 11:12 AM   #6
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
Quote:
Originally posted by spaaarky21
Thanks to both of you.
I am not sure I deserve thanks, I gave you a solution to find running processes, not the number of processors
 
Old 11-20-2005, 11:25 AM   #7
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by shanenin
I am not sure I deserve thanks, I gave you a solution to find running processes, not the number of processors
Hahaha. Actually, I was playing around in the console with the code you both gave and just finished editing my last message when you sent your last reply. However, misunderstanding or not, I appreciate the effort.

-Brandon
 
Old 11-20-2005, 12:36 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Well ... mine is too long :)

grep "^processor" /proc/cpuinfo|wc -l

That's better! :D


Cheers,
Tink
 
Old 11-20-2005, 12:39 PM   #9
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
I like the first verison piping the output to grep. That seems like a more natural way to do it.
 
Old 11-20-2005, 01:20 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I'm still piping it :) ... using "cat file | grep" just really makes
no sense ;) ... it's an unnecessary extra command.


Cheers,
Tink
 
Old 11-20-2005, 01:36 PM   #11
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by shanenin
I like the first verison piping the output to grep. That seems like a more natural way to do it.
Seems like there would be less overhead with this new way since grep reads from /proc/cpuinfo directly instead of making unnecessary calls to cat and piping the output. Although I suppose it would only be called once at the beginning of the program so it wouldn't really matter.
 
Old 11-20-2005, 01:44 PM   #12
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
I am sure the second way is more efficient. I think I am just used to doing it the unefficient way.
 
  


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
Preserving number of links with 'cp'. ( preserve number of links ) Devyn Linux - General 7 09-16-2005 10:07 PM
what is a major number and a minor number ? Menestrel Linux - Newbie 2 10-11-2004 07:53 AM
how do you edit your virtual console number? (or VT number 3) jjorloff1 Linux - General 2 04-03-2004 07:21 PM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM
Will Linux Support Amd Processors And Other Than Intel Processors? halovivek Linux - Hardware 6 09-02-2003 01:08 PM

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

All times are GMT -5. The time now is 06:32 PM.

Contact Us - Advertising Info - Rules - Privacy - Donations - Contributing Member - LQ Sitemap - "Weather apps tell you it'll rain. Wyndo tells you when to go."
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