From owner-freebsd-questions Tue Apr 8 09:00:07 1997 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.5/8.8.5) id JAA15168 for questions-outgoing; Tue, 8 Apr 1997 09:00:07 -0700 (PDT) Received: from silvester.zoom.es (root@irc.zoom.es [195.76.150.4]) by freefall.freebsd.org (8.8.5/8.8.5) with ESMTP id JAA15132 for ; Tue, 8 Apr 1997 09:00:03 -0700 (PDT) Received: from zoom129.zoom.es (zoom129.zoom.es [195.76.150.129]) by silvester.zoom.es (8.7.5/8.7.3) with SMTP id RAA02758 for ; Tue, 8 Apr 1997 17:59:09 +0200 Message-Id: <199704081559.RAA02758@silvester.zoom.es> Comments: Authenticated sender is From: "Jesus A. Mora Marin" To: questions@freebsd.org Date: Tue, 8 Apr 1997 17:53:26 +0000 Subject: Becoming a daemon the long way... Reply-to: amora@zoom.es Priority: normal X-mailer: Pegasus Mail for Windows (v2.50) Sender: owner-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk 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