On BSDs, where SC_NPROCESSORS_ONLN is not available, the number of online processors can be retrieved using `sysctl'.
Cheers, Giuseppe >From 73c11226e320c1144887daeb15e3f549b0bb2ee5 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano <gscriv...@gnu.org> Date: Sun, 18 Oct 2009 01:20:14 +0200 Subject: [PATCH] nproc: use `sysctl' when it is available. * lib/nproc.c (num_processors): If `sysctl' is available, use it to get the number of online processors. --- lib/nproc.c | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-) diff --git a/lib/nproc.c b/lib/nproc.c index 9a6e23a..f45feaf 100644 --- a/lib/nproc.c +++ b/lib/nproc.c @@ -34,5 +34,15 @@ num_processors (void) return nprocs; #endif +#ifdef HW_NCPU + int ret; + int mib[2] = {CTL_HW, HW_NCPU}; + long int nprocs; + size_t len = sizeof (nprocs); + ret = sysctl (mib, 2, &nprocs, &len, NULL, 0); + if (ret == 0) + return nprocs; +#endif + return 1; } -- 1.6.3.3