Why is finding endianess non-portable ? How about...
unsigned int i = 1;
char *p;
p = (char *) &i;
if (*p == 0)
{
/* I'm a BIG end machine */
}
else
{
/* I'm a LITTLE end machine */
}
Martin
-----Original Message-----
From: Mike A. Harris [mailto:[EMAIL PROTECTED]]
Sent: 05 September 2000 09:03
To: [EMAIL PROTECTED]
Subject: Re: SMP question ?
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
_______________________________________________
Redhat-devel-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-devel-list