[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.
						--Harry

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 ".")
		     (terpri)
		     (return (beget:wait child)))
		    ((< child 0)
		     ; error branch
		     (princ "Birth pain.")
		     (terpri)
		     (return child))
		    (t
		     ; impossible branch
		     (princ "Impossible pain.")
		     (terpri)
		     (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.")
		     (terpri)
		     (return (car pvec)))
		    (t
		     ; another child died, keep waiting for ours
		     (return (beget:wait child)))))))