SysVR3.2 Setpgrp behaviour

Lloyd Kremer kremer at cs.odu.edu
Tue Oct 17 01:56:09 AEST 1989


In article <253664A4.20995 at ateng.com> chip at 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!



More information about the Comp.unix.wizards mailing list