39 #include "safec_lib.h"
53 char cpuUsage[MAXLEN] = {
'\0'};
57 printf(
"USED_CPU:%s\n", cpuUsage);
61 printf(
"USED_CPU:NOT AVAILABLE\n");
77 long double a[10], b[10],usr_cpu,total_time;
79 char cpuUtilization[MAXLEN]={
'\0'};
85 fp = fopen(
"/proc/stat",
"r");
91 if( fscanf(fp,
"%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
92 &a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9]) != 10 )
93 printf(
"%s:Error in fscanf()\n",__FUNCTION__);
98 fp = fopen(
"/proc/stat",
"r");
104 if( fscanf(fp,
"%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
105 &b[0],&b[1],&b[2],&b[3],&b[4],&b[5],&b[6],&b[7],&b[8],&b[9]) != 10 )
106 printf(
"Error in fscanf()\n");
110 total_time=(b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]+b[8]+b[9])
112 (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]);
114 usr_cpu=((b[0]-a[0])/total_time)*100;
116 rc = sprintf_s(cpuUtilization,
sizeof(cpuUtilization),
"%Lf",usr_cpu);
126 rc = strcpy_s(cpuUtil,MAXLEN,cpuUtilization);
136 printf(
"Exit from getCPUusage due to NULL pointer\n");