Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!samsung!spool2.mu.edu!uunet!sdrc!thor!scjones From: scjones@thor.UUCP (Larry Jones) Newsgroups: comp.unix.questions Subject: Re: Starting a daemon on a SVR3 Message-ID: <104@thor.UUCP> Date: 8 Jan 91 21:32:47 GMT References: <25360@adm.brl.mil> <1991Jan7.120329.787@erbe.se> Organization: SDRC, Cincinnati Lines: 29 In article <1991Jan7.120329.787@erbe.se>, prc@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@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