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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-19-2005, 07:56 PM
|
#1
|
|
Member
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192
Rep:
|
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
|
|
|
|
11-19-2005, 08:07 PM
|
#2
|
|
Member
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192
Original Poster
Rep:
|
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.
|
|
|
|
11-19-2005, 11:27 PM
|
#3
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
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
|
|
|
|
11-19-2005, 11:39 PM
|
#4
|
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Code:
cat /proc/cpuinfo | grep "^processor"|wc -l
Cheers,
Tink
|
|
|
|
11-20-2005, 11:06 AM
|
#5
|
|
Member
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192
Original Poster
Rep:
|
Actually, shanenin, I was looking for a way to get the number of process ors, 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.
|
|
|
|
11-20-2005, 11:12 AM
|
#6
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
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 
|
|
|
|
11-20-2005, 11:25 AM
|
#7
|
|
Member
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192
Original Poster
Rep:
|
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
|
|
|
|
11-20-2005, 12:36 PM
|
#8
|
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Well ... mine is too long :)
grep "^processor" /proc/cpuinfo|wc -l
That's better! :D
Cheers,
Tink
|
|
|
|
11-20-2005, 12:39 PM
|
#9
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
I like the first verison piping the output to grep. That seems like a more natural way to do it.
|
|
|
|
11-20-2005, 01:20 PM
|
#10
|
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
I'm still piping it :) ... using "cat file | grep" just really makes
no sense ;) ... it's an unnecessary extra command.
Cheers,
Tink
|
|
|
|
11-20-2005, 01:36 PM
|
#11
|
|
Member
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192
Original Poster
Rep:
|
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.
|
|
|
|
11-20-2005, 01:44 PM
|
#12
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
I am sure the second way is more efficient. I think I am just used to doing it the unefficient way.
|
|
|
|
All times are GMT -5. The time now is 04:31 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|