Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Discover multiple processors programmatically?



On Dec 18, 2005, at 9:58 PM, Eric Albert wrote:
On Dec 18, 2005, at 5:45 PM, Ivan S. Kourtev wrote:

First, I tried sysconf() but it doesn't seem capable of doing what I need under Mac OS? The two variables
_SC_NPROCESSORS_CONF and _SC_NPROCESSORS_ONLN are undefined in unistd.h -- am I missing something?

They don't seem to be defined on Mac OS X. I'd suggest filing a bug report with Apple (<http://bugreport.apple.com>) if you'd like to see them added.

They are non-standard extensions tot he sysconf namespace. They are unlikely to be included even if a bug report is filed, since that particular namespace belongs to the standards committee; if they come up with the same name that meant something else, we wouldn't be able to implement it correctly because of binary backward compatibility issues, so it's better if we don't add it.


The reason it's in the man page is that our man page is cribbed from FreeBSD, and FreeBSD implements them. Our manual page there is fairly out of data, but man page fixes are unlikely to make it into a software update, for various reasons. The authoritative reference is the contents of unistd.h.

The only ones you can actually use portably between platforms are the ones defined by POSIX (assuming your other platforms are POSIX compliant).

See also <http://www.opengroup.org/onlinepubs/009695399/functions/sysconf.html >.


I also looked into sysctl as per Daniel's and Eric's suggestions -- I noticed even the sysconf manpage suggests that the sysctl interface is much richer. On Mac OS X, I got some code working right away (attached at end) but I haven't figured out how to get it to go under redhat (everything I do must work under both Mac OS X and redhat at least). redhat has a sys/sysctl.h but it only contains the declaration of sysctl() and none of the keywords. I realize this may be a little off-topic, but any clues?

This sounds like a great job for a configure script. This is hardly the only difference between Mac OS X and Linux. :) Another alternative is to do something like
#ifdef HW_NCPU
...do the sysctl thing...
#elif defined(_SC_NPROCESSORS_CONF)
...do the sysconf thing...
#else
#error Uh oh.
#endif

sysctl is the way it should be done. As otherwise noted in this thread already, these particular sysctl entries are generally portable between 4.4BSD based systems.



By the way, in the code below, what are the HW_ keywords (if any) that correspond to the commented out entries in the data[] array? I really only need HW_NCPU and HW_AVAILCPU for now but just out of curiosity?

Not all sysctl entries have numeric items to go along with their names. Sometimes you just have to use sysctlbyname.

And in fact you should use names everywhere you can, rather than OIDs, for forward code compatibility. We are likely to change things in the future, particularly in this area of sysctl, and sysctlbyname will be less fragile. I expect the current values won't change (i.e. suddenly stop working for already compiled code), but the sysctlbyname() is the preferred interface going forward.


-- Terry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Unix-porting mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/unix-porting/email@hidden

This email sent to email@hidden
References: 
 >Discover multiple processors programmatically? (From: "Ivan S. Kourtev" <email@hidden>)
 >Re: Discover multiple processors programmatically? (From: Eric Albert <email@hidden>)
 >Re: Discover multiple processors programmatically? (From: Dave Zarzycki <email@hidden>)
 >Re: Discover multiple processors programmatically? (From: "Ivan S. Kourtev" <email@hidden>)
 >Re: Discover multiple processors programmatically? (From: Eric Albert <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.