Discussion:
calculation of cpu idle time
(too old to reply)
r***@gmail.com
2006-08-23 11:38:13 UTC
Permalink
hi
i'm struggling to calculate idle time of cpu and if idle time is
more then i have to shut down the system. can anyone tell me the idea
to do that please
r***@gmail.com
2006-08-23 12:35:50 UTC
Permalink
Post by r***@gmail.com
hi
i'm struggling to calculate idle time of cpu and if idle time is
more then i have to shut down the system. can anyone tell me the idea
to do that please
Hi Ram,

This would os specific though. I don't think c provides any function
that can be applied at generic level (across all os) to get the cpu
details . However you can use the popen method defined in stdio.h to
get the same

For instance on Linux you can do the following

fp = popen("top", "r");
if (fp == NULL)
/* Handle error */;

while (fgets(path, BUFF, fp) != NULL){
/*fgets would get you the cpu info . You have to do the parsing to
get the CPU info and apply your condition checking */
printf("%s", path);
}

However if you are good at perl then this can be done 10 times easier .
Flash Gordon
2006-08-23 14:41:45 UTC
Permalink
Post by r***@gmail.com
Post by r***@gmail.com
hi
i'm struggling to calculate idle time of cpu and if idle time is
more then i have to shut down the system. can anyone tell me the idea
to do that please
Hi Ram,
This would os specific though. I don't think c provides any function
that can be applied at generic level (across all os) to get the cpu
details . However you can use the popen method defined in stdio.h to
get the same
<snip further off topic advice>

I can't. At least not on all of my systems, since not all of them *have*
a popen function anywhere since it is not part of the C standard.

Since the OP has not specified what system the only sensible advice it
to find a group dedicated to whatever system it is and ask there since
it can't be done in standard C which is what we discus here.
--
Flash Gordon
Loading...