Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 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;        total = cpu_idle + cpu_nonidle;
  50.  
  51.         //differentiate
  52.         total_d = total - prev_total;
  53.         idle_d = cpu_idle - cpu_prev_idle;
  54.  
  55.         cpu_percentage = (total_d - idle_d)/total_d;
  56.  
  57.     return cpu_percentage;
  58. }
  59.  
  60.  
  61. int main(int argc, char *argv[]) {
  62.  
  63.     FILE *f_shell_command;
  64.     int refresh_time = 5;
  65.     bool repeat;
  66.  
  67.                         if ((f_shell_command = fopen("/proc/stat", "r")) == NULL)
  68.                         {
  69.                             fprintf(stderr, "ERROR: Unable to open /proc/stat\n");
  70.                             return 1;
  71.                         }
  72.  
  73.                         pid_t pid;
  74.                         int cpu_percentage;
  75.  
  76.                         pid = fork();
  77.                         if (pid == 0)
  78.                         {
  79.                             while(1)
  80.                             {
  81.                                 cpu_percentage = load(f_shell_command);
  82.                                 printf("cpu percentage is %d\n", cpu_percentage);
  83.                             }
  84.                         }
  85.                         else if (pid < 0)
  86.                         {
  87.                             fprintf(stderr, "Unable to open process\n");
  88.                             return 1;
  89.                         }
  90.                         else
  91.                         {
  92.                             do {
  93.                                 printf("cpu percentage in while is %d\n", cpu_percentage);
  94.                                 sleep(refresh_time);
  95.                             } while(repeat);
  96.  
  97.                         }
  98.  
  99.                         fclose(f_shell_command);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement