Avoiding <exiting> processes on Ultrix

Peter da Silva peter at ficc.uu.net
Mon Apr 10 01:08:09 AEST 1989


The following should help. The returned PID is bogus, but you didn't care
for it anyway, right? I can't return the real pid easily, since exit eats
8 bits of the return value.

dfork()    /* Fork and detach, so you can run stuff from a daemon */
{
    int pid, status;

    switch(pid==fork()) {    /* Create a sacrificial child */
        case -1: /* Failure */

            return -1; /* return error code */

        default: /* In parent */

            while(wait(&status) != pid)    /* find child */
                continue;

            if(status)
                return -1;/* error, assume fork failed */
            else
                return pid;/* fork succeeded -- PID BOGUS */

        case 0:    /* In sacrificial child */

            switch(fork()) {
                case -1:     /* fork failed, tell boss */
                    exit 1;
                case 0:     /* return in child */
                    return 0;
                default:     /* fork succeeds, tell boss */
                    exit 0;  /* And sacrifice child to daemon */
                }
        }
}
-- 
Peter da Silva, Xenix Support, Ferranti International Controls Corporation.

Business: uunet.uu.net!ficc!peter, peter at ficc.uu.net, +1 713 274 5180.
Personal: ...!texbell!sugar!peter, peter at sugar.hackercorp.com.



More information about the Comp.unix.questions mailing list