Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 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.  
  35.  
  36.         sleep(1);
  37.  
  38.     fgets(test, 255, f_proc_stat);
  39.  
  40.         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);
  41.         /*fscanf(f_proc_stat, "%s %d %d %d %d %d %d %d %d %d %d",
  42.             cpu, &user, &nice, &system, &idle, &iowait, &irq, &softirq,
  43.             &steal, &guest, &guest_nice);*/
  44.  
  45.         cpu_nonidle = user + nice + system + irq + softirq + steal;
  46.         cpu_idle = idle + iowait;
  47.  
  48.         prev_total = cpu_prev_idle + cpu_prev_nonidle;        total = cpu_idle + cpu_nonidle;
  49.  
  50.         //differentiate
  51.         total_d = total - prev_total;
  52.         idle_d = cpu_idle - cpu_prev_idle;
  53.  
  54.         cpu_percentage = (total_d - idle_d)/total_d;
  55.  
  56.     return cpu_percentage;
  57. }
  58.  
  59.  
  60. int main() {
  61.  
  62.     FILE *f_shell_command;
  63.     int refresh_time = 5;
  64.     bool repeat;
  65.  
  66.                         if ((f_shell_command = fopen("/proc/stat", "r")) == NULL)
  67.                         {
  68.                             fprintf(stderr, "ERROR: Unable to open /proc/stat\n");
  69.                             return 1;
  70.                         }
  71.  
  72.                         pid_t pid;
  73.                         int cpu_percentage;
  74.  
  75.                         pid = fork();
  76.                         if (pid == 0)
  77.                         {
  78.                             while(1)
  79.                             {
  80.                                 cpu_percentage = load(f_shell_command);
  81.                             }
  82.                         }
  83.                         else if (pid < 0)
  84.                         {
  85.                             fprintf(stderr, "Unable to open process\n");
  86.                             return 1;
  87.                         }
  88.                         else
  89.                         {
  90.                             do {
  91.                                 printf("cpu percentage is %d\n", cpu_percentage);
  92.                                 sleep(refresh_time);
  93.                             } while(repeat);
  94.  
  95.                         }
  96.  
  97.                         fclose(f_shell_command);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement