Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <stdbool.h>
  9. #include <sys/wait.h>
  10. #include <unistd.h>
  11.  
  12. int load(FILE *f_proc_stat)
  13. {
  14.  
  15.     //declaration of variables to be read from /proc/stat
  16.     const char cpu[3];
  17.     int user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
  18.  
  19.     //variables to be computed in the function
  20.     int cpu_idle, cpu_prev_idle;
  21.     int cpu_nonidle, cpu_prev_nonidle;
  22.     int prev_total, total, total_d, idle_d;
  23.     int cpu_percentage;
  24.     char test[255];
  25.  
  26.     fgets(test, 255, f_proc_stat);
  27.  
  28.  
  29.         sscanf(test, "%s %d %d %d %d %d %d %d %d %d %d", cpu, &user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice);
  30.  
  31.         cpu_prev_nonidle = user + nice + system + irq + softirq + steal;
  32.         cpu_prev_idle = idle + iowait;
  33.  
  34.         printf("cpu_prev_nonidle is %d\n", cpu_prev_nonidle);
  35.  
  36.  
  37.         sleep(1);
  38.  
  39.     fgets(test, 255, f_proc_stat);
  40.  
  41.         sscanf(test, "%s %d %d %d %d %d %d %d %d %d %d", cpu, &user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice);
  42.         /*fscanf(f_proc_stat, "%s %d %d %d %d %d %d %d %d %d %d",
  43.             cpu, &user, &nice, &system, &idle, &iowait, &irq, &softirq,
  44.             &steal, &guest, &guest_nice);*/
  45.  
  46.         cpu_nonidle = user + nice + system + irq + softirq + steal;
  47.         cpu_idle = idle + iowait;
  48.  
  49.         prev_total = cpu_prev_idle + cpu_prev_nonidle;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement