LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to get "load average" using a program (https://www.linuxquestions.org/questions/programming-9/how-to-get-load-average-using-a-program-191051/)

t_sunil_kumar 06-08-2004 06:39 AM

how to get "load average" using a program
 
Hi friends,

My system is Red hat linux 6.2, kernel 2.2.14. Through a C program, I want to get the "load average" that is shown by "top" command in its first line (load average: 0.19, 0.36, 0.41)

Please help me if any system call is available to get this.

Regards,
-Sunil

Technoslave 06-08-2004 10:43 AM

cat /proc/loadavg

Hko 06-08-2004 10:51 AM

..and in C that would be something like:
Code:

#include <stdio.h>

#define LEN 14

int main()
{
        FILE *fp;
        char str[LEN+1];

        if ((fp = fopen("/proc/loadavg", "r")) == NULL) {
                  perror("");
                  return 1;
        }
        if (fread(str, 1, LEN, fp) != LEN) {
                  fprintf(stderr, "Error reading loadavg\n");
                  return 2;
        }
        str[LEN] = '\0';
        printf("load average: %s\n", str);
        return 0;
}



All times are GMT -5. The time now is 09:20 PM.