![]() |
Determining the CPU count
I am attempting to write a program in c++ which needs to determine the number of physical cores the system has installed. If it were a linux or pure unix system I would look under /proc/cpuinfo, but Apple was kind enough to not include that in Darwin. Could anyone please point me in the right direction?
Thanks, Jared |
The command:
hwprefs cpu_count will show what you want. (You can of course run a command from C++ via 'system' etc) But what is your higher-level purpose in doing this? E.g. if this is a homework assignment, using the above probably isn't what is desired - you are probably expected to deduce the number of cores via experiments with multi-threading. |
sysctl() can tell you how many processors are available.
But Hayne's main question stands: Why do you want to do this? Generally, you shouldn't care. |
It's for a program of my own I've been working on. It's a multi-process program that I want to run one process per CPU.
Thanks for the speedy help, Jared |
FWIW, I believe sysctl reports number of cores when asking for CPU count on a multi-core CPU. Don't know if it matters to you or not.
|
Actually it is excatly what I needed. Works great, thankyou.
|
getting a value from system()
This is related to my previous posting, but different enough I felt it needed its own thread....
So when I call system(), it normally would print to the screen. I know I can overide this (most likely with pipes) but my recollection of command line unix is fuzzy at best. So lets say I have this code:
Obviously this doesnt give what I want yet. So how might I get the value that currently appears on my console screen to appear in the variable cpuCount? Or am I mistaken on being able to do this at all? Thanks, Jared |
Maybe popen() instead of system()?
However, considering your previous line of questioning, sysctl() would allow you to avoid the shell spawn altogether. |
|
Sorry, didnt realize sysctl() was a C++ function. I have the include file, but I cant seem to make sense of how to turn the unix call
sysctl hw.ncpu into cpuCount = sysctl(arguments here); any help? |
It's not a C++ function, it's a C function that's been in BSD for ages. You can use it in any C, Obj-C, or C++ app. Furthermore, the whole reason we put () next to the name was to distinguish it as a function call. :)
The function itself is a little unwieldy if you haven't used it before. Here's a concise example from cocoa-dev: http://www.cocoabuilder.com/archive/...2001/8/7/49091 It's worth noting that sysctlbyname() may be easier, but the above should work fine. |
thanks. I'll give it a shot. Looks like that should take care of it for me.
|
Worked beautifully. Thanks for the input.
|
Quote:
I've merged the two threads. |
Quote:
|
Quote:
|
Mine thought it was ;)
|
Quote:
|
My compiler is throwing a bunch of warnings about my objects having non-virtual destructors. I havnt had the chance to figure out what that even means yet (I would think it would want the destructors to be non virtual?) let alone fix it so Ive been ignoring the complier warning messages. I did fix the typo though.
|
Quote:
so (assuming that #define), the last argument is: 0 == -1 which is evaluated at compile time to be false, which is numerically equal to 0 which is what would seem appropriate for that argument anyway. So I wouldn't expect any compiler warnings. But the part that works accidentally is that the 'if' statement is testing whether sysctl succeeded. Since it returns 0 upon success, testing for non-zero (true) is sufficient (but sub-optimal stylistically). |
Quote:
http://www.parashift.com/c++-faq-lit....html#faq-20.7 Quote:
The compiler is very polite - it says things like: "Excuse me, Mr Programmer, but I think maybe this thing you are doing on line 73 isn't quite right." when your (C++ knowledgeable) boss would be saying something like: "You ****ing idiot, what the **** are you doing on line 73 ?!?!" You should at least configure the compiler to warn only about the most serious things instead of ignoring all warnings. But usually it is best to ask for max warnings and benefit from the superior wisdom of the compiler. |
| All times are GMT -5. The time now is 06:00 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.