/proc/loadavg on 2.2.x

From: Urs Hengartner (uhengart+@cs.cmu.edu)
Date: Sun Oct 15 2000 - 12:50:09 EST

I have a program that reads and prints /proc/loadavg once per second
(look below). Note that the file is reopened for each read operation.
I'm mainly interested in the fourth value, i.e., the number of processes
in the run queue. Its initial value is one. After starting a cpu-intensive
background process, the value should go up to two, however, the value
actually becomes four. When the background process is terminated, the
value won't go down to one, either, it stays at three. When the monitoring
program is restarted, the correct value is displayed; the same applies for
'cat /proc/loadavg'. In summary, the strange behavior is observed only for
processes opening and reading /proc/loadavg multiple times.

The behavior occurs on stock 2.2.x kernels; on Redhat 2.2.16-22, the
value printed while the background process is running toggles between
two and four, when the background process is terminated, it toggles
between one and three. 2.0.36 behaves as expected.

- Urs

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#define LINUX_LDAV_FILE "/proc/loadavg"
int main()
  FILE *file;
  int count;
  char ldavgbuf[41];
  while(1) {
    file = fopen (LINUX_LDAV_FILE, "r");
    if (!(file))
      return -1;
    memset(ldavgbuf, 0, 41);
    count = fread (ldavgbuf, 1, 40, file);
    (void) fclose (file);
    if (count <= 0)
      return -1;
    fprintf(stderr,"%s\n", ldavgbuf);
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

This archive was generated by hypermail 2b29 : Sun Oct 15 2000 - 21:00:28 EST