LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting the number of processors (https://www.linuxquestions.org/questions/programming-9/getting-the-number-of-processors-384755/)

spaaarky21 11-19-2005 07:56 PM

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

spaaarky21 11-19-2005 08:07 PM

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.

shanenin 11-19-2005 11:27 PM

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


Tinkster 11-19-2005 11:39 PM

Code:

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

Cheers,
Tink

spaaarky21 11-20-2005 11:06 AM

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

shanenin 11-20-2005 11:12 AM

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 :confused:

spaaarky21 11-20-2005 11:25 AM

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 :confused:
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

Tinkster 11-20-2005 12:36 PM

Well ... mine is too long :)

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

That's better! :D


Cheers,
Tink

shanenin 11-20-2005 12:39 PM

I like the first verison piping the output to grep. That seems like a more natural way to do it.

Tinkster 11-20-2005 01:20 PM

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


Cheers,
Tink

spaaarky21 11-20-2005 01:36 PM

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.

shanenin 11-20-2005 01:44 PM

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 06:31 PM.