Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 8 Apr 1997 17:53:26 +0000
From:      "Jesus A. Mora Marin" <amora@zoom.es>
To:        questions@freebsd.org
Subject:   Becoming a daemon the long way...
Message-ID:  <199704081559.RAA02758@silvester.zoom.es>

next in thread | raw e-mail | index | archive | help
Greetings!

These days I have been discussing with a fellow about the right way 
to make a process to run as a daemon. The easy way to do this seems 
to be -in a simplified scheme-:

	main()
	{
		...
		if(fork())	/* let's say it'll never fail! */
			exit(0); /* Parent dies */

		/* child is inherited by init and becomes a daemon */
		here_the_daemonized_code;
	}


Another approach I saw somewhere -and the one I've been using- is a 
bit more complex:

	main()
	{
		...
		if(!fork()) {	/* child */
			if(!fork()) {	/* child's child */
				while(getppid() != 1) sleep(1);
				code_to_daemonize;
				...
			}
			exit(0);
		}
		wait();	/* parent waits to reap the first child */	  
		exit(0);
	}

This approach seems to be aimed to guarantee that the daemon process 
is inherited by init, so there is no chance to become a zombie 
process if it eventually dies. Is this really needed? By the way, 
are there any system related conditions that make zombies to be 
generated?

TIA


----
Jesus A. Mora Marin, MD (aka EA7HAC, ex-EC7DVE)
Email: amora@zoom.es



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199704081559.RAA02758>