home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.misc:4056 comp.unix.questions:13035 comp.unix.programmer:5188
- Newsgroups: comp.unix.misc,comp.unix.questions,comp.unix.programmer
- Path: sparky!uunet!gatech!news.ans.net!cmcl2!prism.poly.edu!kapela
- From: kapela@prism.poly.edu (Theodore S. Kapela)
- Subject: Re: Daemons must never die
- Message-ID: <1992Nov6.142250.19952@prism.poly.edu>
- Organization: Polytechnic University, New York
- References: <id.JVJU.413@ferranti.com> <1992Nov1.114010.464@global.hacktic.nl> <Bx7Ku6.AAq@ns1.nodak.edu>
- Date: Fri, 6 Nov 92 14:22:50 GMT
- Lines: 55
-
- In article <Bx7Ku6.AAq@ns1.nodak.edu> malenovi@plains.NoDak.edu (Nikola Malenovic) writes:
- >
- >the standard way of creating a daemon process is:
- >
- [ TEXT DELETED }
- >
- >/* child continues!!! */
- >setsid();/* become a session leader! */
-
- Most daemons also redirect stderr, stdout, and stdin to /dev/null.
- A dup2(2V) for each of these fits the bill.
-
- >NOTE the following: fork() does not guarantee who will be run first - in
- >case that the parent gets started first, it will kill itself and the child
- >will receive SIGKILL too! then all the forking was futile. in order to be
-
- This is not true. Signals do not necesarily get passed from parent to child.
- If kill(2V) is passed a process id of 0, it sends the signal specified to
- all processes in the calling program's process group. If the pid is
- a positive value, only that process receives the signal. If the pid is
- negative, then all processes in the same process group as the process
- specified by pid will recieve the signal. killpg(2) may also be used
- for this.
-
- >SURE that the child will have time to do setsid() BEFORE the parent dies
- >(and kills the child by killing itself too!) you can either use the 'sure'
- >way - vfork(), which guarantee that the child will be run first, OR have
-
- Ah, but vfork(2) does not *necesarily* guarantee that the parent will block
- until the child exits. With SunOS 4.X, vfork(2) spawns a new process,
- using all of the parent's resources (memory, file descriptors (and the
- objects they reference), etc. . .) With this implementation, the
- parent is blocked until the child process exits (via _exit(2V)
- (*not* exit(2)) or via an execve(2V)). If the child never exits,
- the parent never continues.
-
- With AIX 3.X, vfork(2) is implemented similar to fork(2V): A completely
- new process is started, in its own address space, using none of the parent's
- resources. With this implementation, the parent does *NOT* block, and
- may continue on.
-
- >to loose the CPU, and if the CPU scheduling is round-robin, you hope that
- >between sleep(1) and the next CPU burst time of the parent, the child will
-
- You don't need to worry about this. BSD4.3 NET2 (and BSD4.4) have a
- routine daemon() in libutil that system daemons use. It essentially does
- a fork(), parent exit(), child setsid(), child dup2() once each on
- stdin, stdout, stderr filenos. I have used similar code many times, and
- it is always reliable.
-
- --
- ..............................................................................
- Theodore S. Kapela kapela@poly.edu
- Center for Applied Large-Scale Computing
- Polytechnic University
-