Newsgroups: comp.sys.apollo
Path: utzoo!utgpu!news-server.csri.toronto.edu!helios.physics.utoronto.ca!alchemy.chem.utoronto.ca!system
From: system@alchemy.chem.utoronto.ca (System Admin (Mike Peterson))
Subject: Re: SR10.3 source to determine load average	
Message-ID: <1991Feb27.175858.11414@alchemy.chem.utoronto.ca>
Keywords: load average
Organization: University of Toronto Chemistry Department
References: <1991Feb27.042254.14588@bwdls61.bnr.ca>
Date: Wed, 27 Feb 1991 17:58:58 GMT

In article <1991Feb27.042254.14588@bwdls61.bnr.ca> tonyf@bnr.ca (Tony Farrow) writes:
>I have a program which needs to determine the current
>load average on the system similiar to that which 
>is returned by uptime. Does anyone have any code or ideas
>to which they could point me ?

Here is a routine that I got from comp.sources.unix: Volume 4, Issue 78,
which I have tested on SR10.2 and SR10.3 (and it works!):

main()
{
    float aves[3];
    
    ugetloads(aves);

    printf("And using ugetloads, the load averages are: %.2f, %.2f, %.2f\n",
	    aves[0], aves[1], aves[2]); 
}

/* ugetloads(ls)
 * float ld[3];
 *
 * Puts the 1, 5, and 15 minute load averages in the float
 * array passed to it.  This program calls upon uptime(1)
 * which could have different ways of printing ie. with bsd4.2
 * "   9:34pm  up 11 hrs,  3 users,  load average: 0.25, 0.22, 0.24  "
 *                                notice the commas -- ^ --- ^.
 * while bsd4.1 does not print commas.  The BSD41 define will 
 * take care of this if that is your system, it defaults to
 * the 4.2 version.
 *
 * Author:
 *  John Bien
 *  {ihnp4 | ucbvax | decvax}!trwrb!jsb
 *
 * This routine taken from comp.sources.unix: Volume 4, Issue 78
 */

#include <stdio.h>

FILE *popen();

ugetloads(ld)
float ld[3];
{
    FILE *stream;

    if((stream = popen("/usr/ucb/uptime","r")) == NULL)
	return(-1);

#ifdef BSD41
    fscanf(stream,"%*[^l] load average: %f %f %f", &ld[0],&ld[1],&ld[2]);
#else
    fscanf(stream,"%*[^l] load average: %f, %f, %f", &ld[0],&ld[1],&ld[2]);
#endif BSD41
    pclose(stream);
    return(NULL);
}
-- 
Mike Peterson, System Administrator, U/Toronto Department of Chemistry
E-mail: system@alchemy.chem.utoronto.ca
Tel: (416) 978-7094                  Fax: (416) 978-8775
