infinite loops -- waiting for some event

A

Andrej Hocevar

Hello,
what is the best way of solving cases where one has to wait
(forever, until killed or instructed otherwise) for some event to
occur? E.g., wait for data, monitoring a file? In fact, that task is
not that difficult, but takes over the CPU if the infinite loop
lacks sleep(). I've always thought using this method was somehow
inferior but then found out that even utilities like tail(1) (on
Linux) use it. Or on another occasion, if one uses curses (UNIX
again), which will most likely include an infinite loop, with the
nodelay(...) option turned on. Obviously, there must be a solution to
such cases: with nodelay() turned off, the infinite loop doesn't need
sleep(). Is a daemon, thus a system-dependant solution, the best way
to go or is it unconnected with such a task?

What is the solution behind these methods?

Thanks,

andrej
 
B

Bryan Bullard

calling "sleep()" tells the os to schedule a difference task or thread.
which is what you generally want if that task or thread requires some
criteria met before it can continue. otherwise, if you don't call sleep (or
the like), that task or thread will idle needlessly and waste cpu cycles.

-bryan
 
J

Jack Klein

Hello,
what is the best way of solving cases where one has to wait
(forever, until killed or instructed otherwise) for some event to
occur? E.g., wait for data, monitoring a file? In fact, that task is
not that difficult, but takes over the CPU if the infinite loop
lacks sleep(). I've always thought using this method was somehow
inferior but then found out that even utilities like tail(1) (on
Linux) use it. Or on another occasion, if one uses curses (UNIX
again), which will most likely include an infinite loop, with the
nodelay(...) option turned on. Obviously, there must be a solution to
such cases: with nodelay() turned off, the infinite loop doesn't need
sleep(). Is a daemon, thus a system-dependant solution, the best way
to go or is it unconnected with such a task?

What is the solution behind these methods?

Thanks,

andrej

The C language has no such facilities. All of the things you are
asking about are non-standard, platform specific extensions. You need
to ask in a group that supports your particular flavor of UNIX, or

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
K

Keith Thompson

Bryan Bullard said:
calling "sleep()" tells the os to schedule a difference task or thread.
which is what you generally want if that task or thread requires some
criteria met before it can continue. otherwise, if you don't call sleep (or
the like), that task or thread will idle needlessly and waste cpu cycles.

On some systems. The sleep() function is not defined in the C standard.
 
M

Mark McIntyre

What systems can detect busy idling? How do they deal with it?

<OT> Most sensible ones, and they deal with it by switching the task
out. After all if its just twiddling its thumbs for 30 secs, then its
sensible to swap it to disk for that time less a bit.
 
K

Keith Thompson

Mark McIntyre said:
<OT> Most sensible ones, and they deal with it by switching the task
out. After all if its just twiddling its thumbs for 30 secs, then its
sensible to swap it to disk for that time less a bit.

<OT> How can a sensible system tell the difference between a program
"twiddling its thumbs" and doing actual work? If a program contains
something like the following:

for (i = 0; i < SOME_BIG_NUMBER; i ++) {
/* do nothing; this loop should take about 30 seconds */
}

or even

/* non-portably assume that time_t counts seconds */
thirty_seconds_from_now = time() + 30;
while (time() < thirty_seconds_from_now) {
/* do nothing */
}

I'm skeptical that a typical (or any) operating system will be able to
figure out what the program is trying to do and swap it out for 30
seconds. Replacing the /* do nothing */ with some computation makes
this even harder.

(In the first case, of course, the compiler is free to replace the
loop with a single assignment to i, unless i is volatile.)

Or do you mean something else by "busy idling"?
 
M

Mark McIntyre

<OT> How can a sensible system tell the difference between a program
"twiddling its thumbs" and doing actual work?

With the examples you use, it can't. Thats why you should not use
such methods to attempt to be idle. The OS will provide facilities for
twiddling thumbs.
Or do you mean something else by "busy idling"?

The rest state of programmers when the PHB is not in the room. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top