Re: how to set priority for idle process ?

Olaf Titz (olaf@bigred.inka.de)
Sun, 13 Apr 1997 14:33:22 +0200


> I'd like to have a processes doing real and reasonable computations
> only and really only if nothing else is running on the system.
> it should not even try to compete with jobs running with "nice 19".

Here's the "conventional" way of doing this:

#define INTERVAL 30 /* seconds */
#define MAXLOAD 20 /* hundredths */

while (my loop condition) {
while (notidle())
sleep(INTERVAL);
do my computations;
}

void notidle(void)
{
char buf[4];
int f=open("/proc/loadavg", O_RDONLY);
if ((f<0) || (read(f, buf, 4)!=4))
return -1;
close(f);
return ((buf[0]!='0') || (atoi(buf+2))>MAXLOAD);
}

Granted, it still is a non-beautiful busy loop, but comes rather close
to true idle. Four syscalls every 30 seconds aren't that much. :-)

olaf