Re: Writing a daemon that can be used with service ?
- From: "Lew Pitcher" <lpitcher@xxxxxxxxxxxx>
- Date: 6 Dec 2006 10:40:15 -0800
dom wrote:
I'm confused...
This web site tell you how to write a daemon:
http://www.linuxprofilm.com/articles/linux-daemon-howto.html#s1
This looks like it produces a straight executable with an infinite while
loop. What it doesn't say is how to stop the daemon gracefully.
The web page you pointed to /does/ cover "how to write a daemon", in
that everything up to the "while (1)" loop is what you need to do to
make a daemon.
However, the page misleads by giving you the "while(1)" loop as the
active body of the daemon. Substitute what ever logic you want the
daemon to do for that loop and you'll be OK.
Most probably, you'll want the daemon to do something repeatedly. For
that, you /would/ code a loop. However, you would code the loop so that
it contains a reasonable exit or termination condition and process.
This /may/ be a
while (test_for_exit_condition())
loop
or an infinite loop, as you prefer. If you use an infinite loop, you
are mostly restricted to implementing your termination and exit logic
within a signal handler or other asynchronously invoked callback.
You /can/ use signals and a signal handler to set the value that the
test_for_exit_condition() test looks at, or you can use some other
input instead. You may choose to permit the client to issue a shutdown
command through whatever internal channel the daemon opens normally,
and take that value as the true value for test_for_exit_condition().
OTOH, you may look for the presence or absence of a sentinal file in
the test_for_exit_condition() logic, and return true or false depending
on that external condition. In any event, the termination process, like
the "work" of the daemon, is up to you
HTH
--
Lew
.
- References:
- Prev by Date: Re: gdb help: debugging a segfault in boost::shared_ptr
- Next by Date: Re: gdb help: debugging a segfault in boost::shared_ptr
- Previous by thread: Re: Writing a daemon that can be used with service ?
- Next by thread: Re: Writing a daemon that can be used with service ?
- Index(es):
Relevant Pages
|