Xref: utzoo comp.bugs.sys5:1164 comp.unix.wizards:18715 Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uwm.edu!gem.mps.ohio-state.edu!uakari.primate.wisc.edu!xanth!kremer From: kremer@cs.odu.edu (Lloyd Kremer) Newsgroups: comp.bugs.sys5,comp.unix.wizards Subject: Re: SysVR3.2 Setpgrp behaviour Summary: Wouldn't one fork be enough? Message-ID: <10199@xanth.cs.odu.edu> Date: 16 Oct 89 15:56:09 GMT References: <1970@oid.UUCP> <1273@virtech.UUCP> <253664A4.20995@ateng.com> Followup-To: comp.bugs.sys5 Organization: Old Dominion University, Norfolk, Va. Lines: 45 In article <253664A4.20995@ateng.com> chip@ateng.com (Chip Salzenberg) writes: >You should also do another fork() AFTER the setpgrp(). Otherwise, the child >process remains a process group leader. And if a process group leader >without a controlling tty opens a tty that no other process has open at the >time [inhale], then that tty becomes the process's controlling tty. So the >child must fork() again; then the grandchild process is a safe daemon. > >Finally: > > /* > * Daemon code. > * Insert error checking to taste. > */ > > if (fork() != 0) > exit(0); > setpgrp(); > if (fork() != 0) > exit(0); > > /* > * And away we go... > */ Do you need two forks? Wouldn't the "second" fork alone be sufficient? Example: setpgrp(); /* original parent divorces controlling tty and becomes process group leader */ close(0); close(1); close(2); /* sure, why not? */ if(fork() != 0) exit(0); /* death of parent backgrounds the process */ /* child process will be non-process-group-leader */ /* And away we go... */ -- Lloyd Kremer ...!uunet!xanth!kremer Have terminal...will hack!