C function to get the machine load average 
Author Message
 C function to get the machine load average

Hello,

I'm running Unix (Linux) on a machine and I want to get the load average
of the machine in one of my C progs.
I know a function exists (I read it in litterature but cannot remember
where).

Does anyone know ?

TIA,

Lionel


                                                __
    /~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/  \
   |             Lionel Tressens              |____|
   |   INSA Toulouse - Computer Science Dpt   |

   |     http://www.*-*-*.com/ ;  |
   |       Tel : (+33) 6 10 13 35 92          |
   |                                          |
   |  Protect the nature :                    |
   |  This mail was made from recycled bits   |
/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/   |
___________________________________________/



Mon, 15 Oct 2001 03:00:00 GMT  
 C function to get the machine load average

Quote:
> I'm running Unix (Linux) on a machine and I want to get the load average
> of the machine in one of my C progs.
> I know a function exists (I read it in litterature but cannot remember
> where).

uptime gives you the load avg. Or (if you want to do something in your
program depending on the value, and assuming you have a proc filesystem)
you can read /proc/loadavg as in:

#include <stdio.h>

static int
getloadavg (double *l1, double *l2, double *l3)
{
  FILE *infile;
  double dummy;
  int count;
  if (l1 == NULL)
    l1 = &dummy;
  if (l2 == NULL)
    l2 = &dummy;
  if (l3 == NULL)
    l3 = &dummy;
  if (NULL == (infile = fopen ("/proc/loadavg", "r")))
    return 0;
  count = fscanf (infile, "%lf%lf%lf", l1, l2, l3);
  fclose (infile);
  return count == 3;

Quote:
}

int
main (void)
{
  double l1;
  double l2;
  double l3;
  if (!getloadavg (&l1, &l2, &l3))
    return 1;
  else
    printf ("Load averages: %f %f %f.\n", l1, l2, l3);
  return 0;

Quote:
}

There's more possibly useful info in the /proc filesystem as well.
--



Mon, 15 Oct 2001 03:00:00 GMT  
 C function to get the machine load average

Quote:

> Hello,

> I'm running Unix (Linux) on a machine and I want to get the load average
> of the machine in one of my C progs.
> I know a function exists (I read it in litterature but cannot remember
> where).

> Does anyone know ?

> TIA,

> Lionel



You asked on a newsgroup, ( admittedly the wrong one! ) so I'll post my
answer here. Look into the /proc filesystem. For more info try:





Mon, 15 Oct 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. getting the load average

2. Help Please-need algorithm for getting 9 point average in 2d array

3. Is there a function for calculating average

4. Newbie: separate big .cs file into small .cs files

5. Bitmaps don't load on some machines

6. Bitmaps don't load on some machines

7. Getting a C# .Net application to run on a non developer machine

8. Getting IP addr of machine

9. Getting accounts on the UMIACS Parallel Processing machines

10. Getting 320x200x256 on my machine

11. getting machine specific identifier information (SN or other) ???

 

 
Powered by phpBB® Forum Software