Re: How to make a C program work as a Daemon

From: James T. Dennis (jadestar_at_idiom.com)
Date: 11/26/03

  • Next message: Peter C. Tribble: "Re: How can I measure the network load ?"
    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
    

  • Next message: Peter C. Tribble: "Re: How can I measure the network load ?"

    Relevant Pages

    • is this possible
      ... is it possible to fork() a child and then make it independent from the ... i think if i made it a daemon, what i generally need, then it would be no ...
      (comp.unix.programmer)
    • Re: is this possible
      ... > is it possible to fork() a child and then make it independent from the ... > i think if i made it a daemon, what i generally need, then it would be no ...
      (comp.unix.programmer)
    • Re: Is there a religion gene?
      ... by those we are programmed to respect, parents, elders, etc. ... the argument about child brains and religion. ... be eaten by crocodiles" is good advice but "If you don't sacrifice a goat at ... Some of us throw off that programming more easily than others, ...
      (uk.philosophy.atheism)
    • Re: single daemon startup
      ... simple guy programming a daemon that runs just one copy. ... So you may create a virtual clock owned by a process group. ...
      (comp.unix.programmer)
    • Re: single daemon startup
      ... simple guy programming a daemon that runs just one copy. ... who do care will go on talking about it and we'll thank you to leave ... So you may create a virtual clock owned by a process group. ...
      (comp.unix.programmer)

    Loading