On Mon, 4 Sep 2000, Mikael Aronsson wrote:

>Yes I know, but what I was looking for was a portable system call that could
>be used from inside an application to detect the number of CPU's, how about
>other UNIX systems, do they have a proc/cpuinfo to ?

This sort of thing is inherently a non-portable problem like
detecting endianness and other such things.  It is unlikely any
portable solution exists or will ever exist.  Adding syscalls for
things like this are a waste of time.

Since I am in a fun sort of coding mood, here you go, it is GPL
licenced:

--- howmanycpus.c -----------------------------------------------------
/* howmanycpus.c
** Copyright 2000  - by Mike A. Harris <[EMAIL PROTECTED]>
** Licence: GPLv2
*/
#include <stdio.h>
#include <string.h>

#define BUFSIZE 40

int numberofcpus(void);

int main(void)
{
    int cpus;

    cpus = numberofcpus();

    printf("This system has %d processor%s",
            cpus, (cpus == 1) ? "\n" : "s\n");

    return cpus;
}

int numberofcpus(void)
{
    int cpucount = 0;
    char buffer[BUFSIZE];
    FILE *infile;

    if( (infile = fopen("/proc/cpuinfo", "r")) == NULL )
    {
        return -1;
    }

    while( fgets(buffer, BUFSIZE, infile) != NULL)
    {
        if(!strncmp(buffer, "processor", 9))
        {
            cpucount++;
        }
    }

    fclose(infile);
    return cpucount;
}


Here is sample output from a single CPU machine:

2 root@asdf:~# gcc -Wall  -o howmanycpus howmanycpus.c
2 root@asdf:~# howmanycpus
This system has 1 processor

And from a dual processor machine:

SOURCEFORGE mikeharris@orbital:~$ gcc -Wall -o howmanycpus howmanycpus.c
SOURCEFORGE mikeharris@orbital:~$ ./howmanycpus
This system has 2 processors


Enjoy.
TTYL

--
Mike A. Harris                                     Linux advocate     
Computer Consultant                                  GNU advocate  
Capslock Consulting                          Open Source advocate

Red Hat FAQ tip: Having trouble upgrading RPM 3.0.x to RPM 4.0.x?  Upgrade 
first to version 3.0.5, and then to 4.0.x.  All packages are available on 
Red Hat's ftp sites:       ftp://ftp.redhat.com  ftp://rawhide.redhat.com



_______________________________________________
Redhat-devel-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-devel-list

Reply via email to