home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / lisp / lispnews / text0344.txt < prev    next >
Encoding:
Text File  |  1985-11-10  |  1.4 KB  |  57 lines

  1. It is a characteristic of Unix that processes do not really die
  2. until they are waited for.  Your `zombie' process will not die
  3. until you (wait) for it.  The (wait) function returns the dotted
  4. pair (pid . status).  Thus the following examples will spawn
  5. children that immediately die.
  6.                         --Harry
  7.  
  8. In simplest terms:
  9.  
  10.     (def beget
  11.       (lambda nil
  12.     (cond ((fork) (wait))
  13.           (t (exit 0)))))
  14.  
  15. In more realistic terms:
  16.  
  17.     (def beget
  18.       (lambda nil
  19.     (prog (child)
  20.           (setq child (fork))
  21.           (cond ((null child)
  22.              ; child branch: (fork) evaluated to nil
  23.              (exit 0))
  24.             ((> child 0)
  25.              ; parent branch: (fork) evaluated to pid
  26.              (princ "Begot ")
  27.              (princ child)
  28.              (princ ".")
  29.              (terpri)
  30.              (return (beget:wait child)))
  31.             ((< child 0)
  32.              ; error branch
  33.              (princ "Birth pain.")
  34.              (terpri)
  35.              (return child))
  36.             (t
  37.              ; impossible branch
  38.              (princ "Impossible pain.")
  39.              (terpri)
  40.              (return -1))))))
  41.     (def beget:wait
  42.       (lambda (child)
  43.     (prog (pvec)
  44.           (setq pvec (wait))
  45.           (cond ((= (car pvec) child)
  46.              ; child we are waiting for died
  47.              (return (cdr pvec)))
  48.             ((< (car pvec) 0)
  49.              ; error
  50.              (princ "Wait error.")
  51.              (terpri)
  52.              (return (car pvec)))
  53.             (t
  54.              ; another child died, keep waiting for ours
  55.              (return (beget:wait child)))))))
  56.  
  57.