[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problems Forking Around

It is a characteristic of Unix that processes do not really die
until they are waited for.  Your `zombie' process will not die
until you (wait) for it.  The (wait) function returns the dotted
pair (pid . status).  Thus the following examples will spawn
children that immediately die.

In simplest terms:

    (def beget
      (lambda nil
	(cond ((fork) (wait))
	      (t (exit 0)))))

In more realistic terms:

    (def beget
      (lambda nil
	(prog (child)
	      (setq child (fork))
	      (cond ((null child)
		     ; child branch: (fork) evaluated to nil
		     (exit 0))
		    ((> child 0)
		     ; parent branch: (fork) evaluated to pid
		     (princ "Begot ")
		     (princ child)
		     (princ ".")
		     (return (beget:wait child)))
		    ((< child 0)
		     ; error branch
		     (princ "Birth pain.")
		     (return child))
		     ; impossible branch
		     (princ "Impossible pain.")
		     (return -1))))))
    (def beget:wait
      (lambda (child)
	(prog (pvec)
	      (setq pvec (wait))
	      (cond ((= (car pvec) child)
		     ; child we are waiting for died
		     (return (cdr pvec)))
		    ((< (car pvec) 0)
		     ; error
		     (princ "Wait error.")
		     (return (car pvec)))
		     ; another child died, keep waiting for ours
		     (return (beget:wait child)))))))