1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <stdlib.h>
#include <stdio.h>
#ifdef __GLIBC__
#include <sys/sysinfo.h>
#elif defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
/* supposedly,
#if defined(PTW32_VERSION) || defined(__hpux)
return pthread_num_processors_np();
but I cannot verify that at the moment
*/
#if defined(__GLIBC__)
int ncpus(void) { return get_nprocs(); }
#elif defined(__APPLE__) || defined(__FreeBSD__)
int ncpus(void)
{
int count;
size_t size=sizeof(count);
return sysctlbyname("hw.ncpu",&count,&size,NULL,0) ? 0 : count;
}
#else
int ncpus(void) { return 0; } /* "Don't know */
#endif
/* Supposedly, sysconf() can also be used in some cases:
#include <unistd.h>
int const count=sysconf(_SC_NPROCESSORS_ONLN);
return (count>0)?count:0;
*/
|