This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: how to get the number of CPUs?


Bharathi S. wrote:

> On Thu, 4 Jul 2002, Cong wrote:
> > number of CPUs on each of them. So I want to get the number of CPUs
>   cpu info is avilable in /proc/cpuinfo file
>   if processor is 0 then No. of CPU is 1
>   Explore /proc dir you will more idea :)

Or the sysconf solution - for Solaris, at least, but this ought to be
portable:

    #include <stdio.h>
    #include <unistd.h>

    int main(void)
    {
        long nProcessorsConfigured = sysconf(_SC_NPROCESSORS_CONF);
        long nProcessorsOnline     = sysconf(_SC_NPROCESSORS_ONLN);

        if (nProcessorsConfigured != -1)
        {
            printf("Processors configured :
%ld\n",nProcessorsConfigured);
        }
        else
        {
            printf("Unable to read configured processor count.\n");
        }

        if (nProcessorsOnline != -1)
        {
            printf("Processors online     : %ld\n",nProcessorsOnline);
        }
        else
        {
            printf("Unable to read online processor count.\n");
        }

        return 0;
    }

Try 'man sysconf' to get a list of supported sysconf arguments and their
descriptions on your platform.

Rup.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]