daemonize
daemonize copied to clipboard
Wait for the first child to fork before exiting
Without the waitpid() it is possible that the setsid() has not completed yet when the main process terminates, potentially causing a race condition:
If setsid() has not completed and the controlling terminal is closed, the session leader will be sent a SIGHUP signal. By default this terminates the process and then all processes in the session group will also receive a SIGHUP.