Getting rid of a <defunct> process

David Goodenough dg at lakart.UUCP
Tue Feb 14 09:14:02 AEST 1989


>From article <102 at avatar.UUCP>, by kory at avatar.UUCP (Kory Hamzeh):
> I have written an application which forks and execs off many subtasks.
> The main process (the parent which does all of the forks) can not
> do a wait() because I can't get blocked for anything. Well, this results
> in a lot of "<defunct>" processes in the process table when the child
> exits.

This may provide a workable solution, but there is a disadvantage:
It will slow down the fork process, because two fork calls are required
instead of 1. However, that may or may not be an issue.
You may need to muck with the calling parameters passed to forknew, I've
assumed useage of execv() - modify as appropriate for whatever you're
using.

forknew(prog, argv)
char *prog, **argv;
 {
    int pid1, pid2;

    if (fork() != 0)		/* parent */
     {
	wait(&pid1);		/* wait for child */
	return;			/* and carry on */
     }
    if (fork() != 0)		/* child forks AGAIN, */
      exit(0);			/* child exits, so parent's wait()
				 * doesn't block */
    execv(prog, argv);		/* grandchild actually does the execv, but
				 * since it has no parent it gets inherited
				 * by init (PID 1) which will detect it's
				 * passing, and do a wait */
 }

This works on BSD, but may well work on other systems

Comments anyone?
-- 
	dg at lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+



More information about the Comp.unix.questions mailing list