[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: number of processors
From: |
Eric Siegerman |
Subject: |
Re: number of processors |
Date: |
Tue, 27 Aug 2002 14:23:30 -0400 |
User-agent: |
Mutt/1.2.5i |
On Tue, Aug 27, 2002 at 06:00:14PM +0100, Philip Willoughby wrote:
> Today, Philipp Gortan wrote:
>
> >case "$MACHTYPE" in
> >~ i686-*-linux) test -f /proc/cpuinfo && \
> > PROC_NR=`grep processor /proc/cpuinfo | \
> > wc -l | awk '{print $1}'`
> > ;;
>
> This should work for *-*-linux* not just i686s.
It might make sense to just run through the various tests
/proc/cpuinfo without conditionalizing on $MACHTYPE, at least for
the tests which seem unambiguous[*]. That way, if some O/S you
don't know about provides the info in a way you already do know
about, the script will work there.
* E.g. I'd unconditionally trust the test you show for Solaris,
since it explicitly labels the value "NumCPU". But maybe not
the Linux one -- a different O/S could provide a /proc/cpuinfo
with sufficiently different, but similar-looking, contents to
break this.
> I'd suggest the following C code be used:
>
> #include <unistd.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main() {
> long nprocs;
> nprocs = sysconf(_SC_NPROCESSORS_ONLN);
> if (nprocs < 1)
> nprocs = 1;
Rather:
if (nprocs == -1) /* Value unavailable */
exit(EXIT_ERROR);
else if (nprocs == 0)
nprocs = 1;
> printf ("%ld\n",nprocs);
> exit (EXIT_SUCCESS);
> }
Then if it exits with an error, proceed to your other tests.
--
| | /\
|-_|/ > Eric Siegerman, Toronto, Ont. address@hidden
| | /
Anyone who swims with the current will reach the big music steamship;
whoever swims against the current will perhaps reach the source.
- Paul Schneider-Esleben