Re: How to make a C program work as a Daemon
From: James T. Dennis (jadestar_at_idiom.com)
Date: 11/26/03
- Previous message: Gregory L. Hansen: "Re: A few questions about .forward."
- In reply to: Maxim Belushkin: "Re: How to make a C program work as a Daemon"
- Next in thread: Floyd Davidson: "Re: How to make a C program work as a Daemon"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 26 Nov 2003 07:43:05 -0000
In comp.unix.admin Maxim Belushkin <m.belushkin@fz-juelich.de> wrote:
> The simplest way is to double-fork. fork, then get the child process
> to fork again. Let's call this third process a grandchild. Once the
> child fork()s successfully, it should die. The original parent should
> waitpid() right after forking, and die with the child. The grandchild
> is orphaned and now belongs to the init process, and is thus a pure
> daemon process.
This mostly handles the disposition of signals. However you should also
call setsid() (creating a new "session ID" ... or process group) to
completely dissociate signal propagation to your processes.
In general you should also close stdin, stdout, and stderr and open
/dev/null for reading on stdin; and /dev/null for write, fduping copies
for stdout and stderr. (Providing all three of the standard file descriptors
for any libraries to which you link that might try to use them in the
conventional ways).
There are several other small details that you should normally concern
yourself with to make a proper daemon.
However, there are thousands of small daemon programs written in C
and published under the BSD license. Perhaps you should consider
studying some of those. (You can *study* the GPL code, too; but should
avoid making any derivative works --- thus it would be easier and wiser
to simply code under a BSD license to avoid any appearance of "viral
contamination"). (Please! Let's not get into a licensing flamewar here;
nor into a debate on the relative distinctions between studying code and
deriving from it).
You should also go read the UNIX Network Programming book by the late
Richard P. Stevens. It has recently been updated by some other authors
as (apparently) permitted and contracted by Prentice-Hall.
http://www.kohala.com/start/unpv12e.html
http://www.amazon.com/exec/obidos/tg/detail/-/013490012X/102-2943240-2084931?v=glance
That is considered to be the canon for this topic.
Finally, you should be more restrained in your cross-posting.
This really had little or nothing to do with c.u.admin!
> - Time -
> Madhavi wrote:
>> Hi,
>> I know some basic C programming,
>> but I do not know the advanced concepts and new to
>> network programming. Now, I'm assigned the responsibility
>> of writing a C program as a Daemon. I've my C program
>> ready.(It connects to Vertex database and performs
>> tax calculations).I do not know how to make that to
>> work as a daemon.Can you please send me a basic program
>> to get started.
>> Any documentation in this regard will be very helpful.
>>
>> Really appreciate your help
>>
>> Kind Regards
>> Madhavi
> --
> Max Belushkin
-- Jim Dennis, Starshine: Signed, Sealed, Delivered
- Previous message: Gregory L. Hansen: "Re: A few questions about .forward."
- In reply to: Maxim Belushkin: "Re: How to make a C program work as a Daemon"
- Next in thread: Floyd Davidson: "Re: How to make a C program work as a Daemon"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|