![]() |
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 |
Re: Getting the number of processors
Quote:
|
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 |
Code:
cat /proc/cpuinfo | grep "^processor"|wc -lCheers, Tink |
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 |
Quote:
|
Quote:
-Brandon |
Well ... mine is too long :)
grep "^processor" /proc/cpuinfo|wc -l That's better! :D Cheers, Tink |
I like the first verison piping the output to grep. That seems like a more natural way to do it.
|
I'm still piping it :) ... using "cat file | grep" just really makes
no sense ;) ... it's an unnecessary extra command. Cheers, Tink |
Quote:
|
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. |