The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   Determining the CPU count (http://hintsforums.macworld.com/showthread.php?t=97619)

jwoolsto 01-11-2009 06:11 AM

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

hayne 01-11-2009 11:23 AM

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.

Mikey-San 01-11-2009 03:15 PM

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.

jwoolsto 01-11-2009 03:50 PM

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

Mikey-San 01-11-2009 06:06 PM

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.

jwoolsto 01-12-2009 01:55 AM

Actually it is excatly what I needed. Works great, thankyou.

jwoolsto 01-12-2009 02:29 AM

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:


int cpuCount = 0;
int returnValue = 0;
returnValue = system("hwprefs cpu_count")
if (returnValue == -1)
perror("Error determining number of cores on a Mac");
else if (returnValue == 127)
perror("Error opening system console for determination of number of cores on a Mac.");
else
cpuCount = returnValue;
cout << endl << "CPU Count is: " << cpuCount;
return cpuCount;


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

dmacks 01-12-2009 03:10 AM

Maybe popen() instead of system()?

However, considering your previous line of questioning, sysctl() would allow you to avoid the shell spawn altogether.

Mikey-San 01-12-2009 04:34 AM

http://forums.macosxhints.com/showthread.php?t=97619

jwoolsto 01-12-2009 05:05 AM

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?

Mikey-San 01-12-2009 05:22 AM

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.

jwoolsto 01-12-2009 05:25 AM

thanks. I'll give it a shot. Looks like that should take care of it for me.

jwoolsto 01-12-2009 05:59 AM

Worked beautifully. Thanks for the input.

hayne 01-12-2009 07:51 AM

Quote:

Originally Posted by jwoolsto (Post 512803)
This is related to my previous posting, but different enough I felt it needed its own thread....

Since it's a follow-on question, it's better to keep it in the same thread (one of the most important parts of a question is the reason why it is being asked.
I've merged the two threads.

dmacks 01-12-2009 02:57 PM

Quote:

Originally Posted by Mikey-San (Post 512825)
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.

...if one fixes the typo (boolean test of sysctl() return is inside its parameter list!)

Mikey-San 01-12-2009 06:19 PM

Quote:

Originally Posted by dmacks (Post 512932)
...if one fixes the typo (boolean test of sysctl() return is inside its parameter list!)

Maybe your compiler just isn't smart enough to read your mind. :P

jwoolsto 01-12-2009 06:24 PM

Mine thought it was ;)

Mikey-San 01-12-2009 06:38 PM

Quote:

Originally Posted by jwoolsto (Post 512991)
Mine thought it was ;)

Well, it's really just an accident that it works. "NULL == -1" should throw a compiler warning, but it still might "work" because sysctl() probably ignores the newlen argument when newp is NULL. You should still fix it. :)

jwoolsto 01-12-2009 06:41 PM

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.

hayne 01-12-2009 07:25 PM

Quote:

Originally Posted by Mikey-San (Post 512992)
Well, it's really just an accident that it works. "NULL == -1" should throw a compiler warning, but it still might "work" because sysctl() probably ignores the newlen argument when newp is NULL.

NULL is often #define'd to be 0
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).

hayne 01-12-2009 07:34 PM

Quote:

Originally Posted by jwoolsto (Post 512994)
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?)

See:
http://www.parashift.com/c++-faq-lit....html#faq-20.7

Quote:

Ive been ignoring the complier warning messages
That's almost always a big mistake.
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.