i'm new to c coding and stuff.this code i wrote to calculate the cpu utilization.
but now i want the cpu utilization of a particular process when i run it based on its pid. suppose a get the pid of particular process from the cmd>tasklist
now when i use that as input, i should get the cpu utilization.
getpid() returns the process id but i dont need it.
someone help me please
thanks in advance
here is the code.
Code:
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
int main(void)
{
int n;
for(;;)
{
printf("process pid:%d",getpid());
printf("\n");
printf("CPU Usage: %3d%%\r",CPUusage());
printf("\n");
sleep(500);
}
return 0;
}
CPUusage()
{
clock_t start, end;
double elapsed;
double P;
int i,j,k,m;
start = clock();
for( i = 0 ; i < 1000 ; i++)
for( j = 0 ; j < 100 ; j++)
for( k = 0 ; k < 50 ; k++)
for(m=0; m < 5;m++);
end = clock();
elapsed = (((double) (end - start)) / CLOCKS_PER_SEC);
P = elapsed*100;
return(P);
}