home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / wizards / 3606 < prev    next >
Encoding:
Text File  |  1992-08-13  |  5.3 KB  |  155 lines

  1. Newsgroups: comp.unix.wizards
  2. Path: sparky!uunet!wupost!sdd.hp.com!caen!hellgate.utah.edu!fcom.cc.utah.edu!cs.weber.edu!terry
  3. From: terry@cs.weber.edu (A Wizard of Earth C)
  4. Subject: Re: Queueing of signals
  5. Message-ID: <1992Aug14.072625.26349@fcom.cc.utah.edu>
  6. Keywords: SIGCHLD, signals, queueing, sigvec
  7. Sender: news@fcom.cc.utah.edu
  8. Organization: Weber State University  (Ogden, UT)
  9. References: <1992Aug13.160824.11077@bwdls61.bnr.ca>
  10. Date: Fri, 14 Aug 92 07:26:25 GMT
  11. Lines: 142
  12.  
  13. You know, if I had any sense, I'd be going to bed right this instant...
  14.  
  15. In article <1992Aug13.160824.11077@bwdls61.bnr.ca> gwaters@bwdls139.bnr.ca (Glenn Waters) writes:
  16. >I have a program that seems to lose SIGCHLD signals. There are two (or more)
  17. >processes that die simultaneously -- thus generating two SIGCHLDs
  18. >to the parent. I only seem to get the first SIGCHLD.
  19. >
  20. >Are the signals queued? I believe not.
  21. >
  22. >What is the correct method of catching the subsequent child deaths that are
  23. >not queued?
  24.  
  25. 1)    Signals aren't queued
  26. 2)    POSIX signals aren't queued (they almost say they are but not quite)
  27. 3)    You can't catch multiple instances of signals which occur prior/during
  28.     handler invocation, since a signal is a persistant condition, not an
  29.     event.  It may be set by an event (the event itself having been queued),
  30.     but how many time a bit was set high is undetectable.
  31.  
  32.  
  33. HOWEVER:
  34.  
  35. For child deaths, you CAN sneaky around it:
  36.  
  37.  
  38. /*
  39.  * My routine macduff, which handles at least one SIGCLD per cluster of
  40.  * them due to the nature of signals...
  41.  */
  42.  
  43. int    the_bell_tolled = 0;
  44.  
  45. macduff()
  46. {
  47.     /*
  48.      * Mother!  I am killed! --MacDuff in "Macbeth" by W. Shakespere;
  49.      * look it up on your NeXT.
  50.      */
  51.     the_bell_tolled = 1;
  52. }
  53.  
  54. /*
  55.  * My initialization, living somewhere...
  56.  */
  57. ...
  58. signal( SIGCLD, macduff);
  59. ...
  60.  
  61. /*
  62.  * My main loop, living somewhere (preferrably after my initialization 8-)...
  63.  */
  64. for(;;)
  65.     ...
  66.     /*
  67.      * Check for the bell...
  68.      */
  69.     if( the_bell_tolled) {
  70.         /*
  71.          * "Ah! I see you have the machine that goes *BING*!"
  72.          *    - J. Cleese, "The Meaning of Life"
  73.          */
  74.         the_bell_tolled = 0;    /* shut off the bell BEFORE reaping*/
  75.         grim_reaper();        /* ...ask for whom the bell tolled*/
  76.         ...
  77.     }
  78. }
  79.  
  80. /* NOTREACHED*/
  81.  
  82. exit( 0);    /* the end of my program*/
  83.  
  84.  
  85. /*
  86.  * My routine grim_reaper, who takes the souls of the dead child processes
  87.  * to silicon heaven (so to speak, and only in the most general terms).
  88.  *
  89.  * "No silicon heaven!?! Ridiculous!  Where would all the calculators go?"
  90.  *    -- Kryten, "Red Dwarf"
  91.  */
  92. grim_reaper()
  93. {
  94.     int    pid;        /* pid_t for puritans*/
  95.     int    st;
  96.  
  97.     /*
  98.      * You may have to use a different "scythe": ie: wait3 instead of
  99.      * waitpid, or whatever you have instead that returns the pid and
  100.      * status of a reaped child process if the is one but doesn't
  101.      * hang otherwise.
  102.      */
  103.     while( ( pid = waitpid( -1, &st, WNOHANG)) > 0)  {
  104.         printf( "Child PID %d exited with %d\n", pid, st);
  105.         ...
  106.     }
  107. }
  108.  
  109.  
  110.  
  111.     What happens is that one or more child deaths result in a signal
  112. SIGCLD being caught by macduff.  He sets a flag (the_bell_tolls).  Then
  113. SIGCLD's are allowed to happen again.  This way, regardless of the number
  114. of SIGCLDs, you always know that there was 0 > count < n of them.  Since
  115. the handler is not in operation at the time that the actual handling is
  116. to [conditionally] occur, handling the SIGCLD's will not cause them to
  117. be blocked for the duration.  By setting the flag to zero before waking
  118. up Bill and Ted's friend to deal with the handling, we make sure that
  119. anything caught while handling will result in another call to the handler,
  120. and that there is thus not a window where we can "miss" one or more SIGCLDs
  121. (SIGChiLDren?).  This translates the SIGCLD from a "one or more processes
  122. have died" *flag* into a "better see how many died" *event*.  Mr. Reaper
  123. then goes out and reaps each of the child processes.  If you were interested
  124. in precisely which child died (ie: for some reason your children were not
  125. homogeneous processes, like fork/exec), you have the PID of the dead child,
  126. and can do a reverse-lookup to compare against an array of child PIDs (which
  127. are probably structure element members) which you cleverly saved off when
  128. you forked, and can act accordingly.
  129.  
  130.     Delayed handling of signals is useful in any case where the cause
  131. of the signal is determinable and persistant (SIGPOLL on a stream head,
  132. for instance, where determination of the culprit can be made using the
  133. select/poll system calls), but not as a general mechanism.  This means you
  134. should probably not use multiple signal generators for which you are handling
  135. signals (ie: child deaths and stream data available) in the same program
  136. unless you can be guaranteed of a signal being blocked for delivery instead
  137. of ingnored when you're handling the other signal.
  138.  
  139.     Signals are queued in the POSIX 1003.4 spec, but this is still
  140. subject to change and probably wont be frozen one way or the other until
  141. late 93/early 94; until then, we'll all have to just limp along...
  142.  
  143.  
  144.                     Terry Lambert
  145.                     terry_lambert@gateway.novell.com
  146.                     terry@icarus.weber.edu
  147. ---
  148. Any opinions in this posting are my own and not those of my present
  149. or previous employers.
  150. -- 
  151. -------------------------------------------------------------------------------
  152.                                                        terry@icarus.weber.edu
  153.  "I have an 8 user poetic license" - me
  154. -------------------------------------------------------------------------------
  155.