nproc.c (781B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <unistd.h> 6 7 #include "util.h" 8 9 static void 10 usage(void) 11 { 12 eprintf("usage: %s [-a]\n", argv0); 13 } 14 15 // ?man nproc: print the number of available processors 16 // ?man arguments: [-a] 17 // ?man with no options, prints the number of processors currently 18 // ?man online. -a prints the number installed, including any offline 19 int 20 main(int argc, char *argv[]) 21 { 22 int aflag = 0; 23 long n; 24 25 ARGBEGIN 26 { 27 // ?man -a: count installed processors, not just online ones 28 case 'a': 29 aflag = 1; 30 break; 31 default: 32 usage(); 33 } 34 ARGEND; 35 36 n = sysconf(aflag ? _SC_NPROCESSORS_CONF : _SC_NPROCESSORS_ONLN); 37 if (n <= 0) 38 n = 1; 39 printf("%ld\n", n); 40 return 0; 41 }