Back to index

glibc  2.9
smp.h
Go to the documentation of this file.
00001 /* Determine whether the host has multiple processors.  Linux version.
00002    Copyright (C) 1996, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public License as
00007    published by the Free Software Foundation; either version 2 of the
00008    License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00017    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.  */
00019 
00020 #include <sys/sysctl.h>
00021 
00022 /* Test whether the machine has more than one processor.  This is not the
00023    best test but good enough.  More complicated tests would require `malloc'
00024    which is not available at that time.  */
00025 static inline int
00026 is_smp_system (void)
00027 {
00028   static const int sysctl_args[] = { CTL_KERN, KERN_VERSION };
00029   char buf[512];
00030   size_t reslen = sizeof (buf);
00031 
00032   /* Try reading the number using `sysctl' first.  */
00033   if (__sysctl ((int *) sysctl_args,
00034               sizeof (sysctl_args) / sizeof (sysctl_args[0]),
00035               buf, &reslen, NULL, 0) < 0)
00036     {
00037       /* This was not successful.  Now try reading the /proc filesystem.  */
00038       int fd = __open ("/proc/sys/kernel/version", O_RDONLY);
00039       if (__builtin_expect (fd, 0) == -1
00040          || (reslen = __read (fd, buf, sizeof (buf))) <= 0)
00041        /* This also didn't work.  We give up and say it's a UP machine.  */
00042        buf[0] = '\0';
00043 
00044       __close (fd);
00045     }
00046 
00047   return strstr (buf, "SMP") != NULL;
00048 }