Starting a daemon on a SVR3

Larry Jones scjones at thor.UUCP
Wed Jan 9 08:32:47 AEST 1991


In article <1991Jan7.120329.787 at erbe.se>, prc at erbe.se (Robert Claeson) writes:
> I believe that something along these lines should work:
> 
> 	setbuf(stdout,NULL);
> 	printf("Listserv v0.99: daemon started\n\r");
> 	if (fork())		/* Disaccociate us from controlling tty */
> 		exit(0);
> 	setpgrp();		/* Start a new process group */
> 	if (fork())		/* ...and disaccociate us again */
> 		exit(0);
> 
> I only have a vague memory of why the second fork() is needed.

The reason for the second fork is to make sure that you don't get a
controlling terminal.  What happens is the first fork creates a new
process and the setpgrp then causes it to become a process group
leader.  This has the side-effect of disassociating it from its
controlling terminal, since controll tty is a process group related
thing.  The potential problem is that any time a process group
leader without a controlling tty opens a terminal that isn't
already open by someone else, it magically becomes the controlling
tty for that process group.  By forking again, you get rid of the
process group leader and don't have to worry about that happening.
----
Larry Jones                         UUCP: uunet!sdrc!thor!scjones
SDRC                                      scjones at thor.UUCP
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
Do you think God lets you plea bargain? -- Calvin



More information about the Comp.unix.questions mailing list