daemon question

Apr 21, 2012 at 2:28am
i'm writing a daemon that just kinda sits around and waits to handle various signals.

what's the typical way to keep this kind of program running?
is it ok to just write an infinite loop?

ie:
1
2
3
4
5
bool running;
while (running)
{
   //do nothing, just handle signals
}
Apr 21, 2012 at 9:21am
That's an evil loop. NEVER do it like that abstractly!! it occupies your processor fully.

You may use sleep functions, they are available in many C libraries. Add a sleep function to that loop, if your program sleeps 1 second before every check, you'd be doing your processor a favour. If you have a 3 GHz processor, that empty loop is gonna execute 3 Billion times (since it's empty)!!!! So you're really doing your processor a favour with that sleep approach.

And to stop that loop, use a system signal, like the break signal with Ctrl+C.
Topic archived. No new replies allowed.