home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / programm / 5277 < prev    next >
Encoding:
Internet Message Format  |  1992-11-14  |  2.0 KB

  1. Path: sparky!uunet!ukma!wupost!zaphod.mps.ohio-state.edu!rpi!utcsri!robinson
  2. Newsgroups: comp.unix.programmer
  3. From: robinson@mdivax1.uucp (Jim Robinson)
  4. Subject: Re: Signal Handlers and malloc()
  5. Message-ID: <1992Nov14.002221.5672@mdivax1.uucp>
  6. Reply-To: robinson@mdd.comm.mot.com (Jim Robinson)
  7. Organization: Motorola - Mobile Data Division; Richmond, BC
  8. X-Newsreader: TIN [version 1.1 PL6]
  9. References: <BxIL1s.4py@flatlin.ka.sub.org>
  10. Date: 14 Nov 92 00:24:32 GMT
  11. Lines: 47
  12.  
  13. Christoph Badura (bad@flatlin.ka.sub.org) wrote:
  14. >In <1992Nov9.064910.778@noose.ecn.purdue.edu> kudva@ecn.purdue.edu (Gautham K. Kudva) writes:
  15. >
  16. >>     I've written a signal handler for the VTALRM signal. The signal
  17. >>handler calls another routine which does quite a bit of work. This program
  18. >>start crashing in an unpredictable fashion.
  19. >
  20. >You should rewrite your program so that it only sets a flag variable
  21. >in the signal handler and do the "quite a bit of work" out side the
  22. >signal handler in the main part of the program.
  23. >
  24. >This is the only way to make the program work reliably.
  25.  
  26. If your program is structured as some sort of I/O loop, then there is an
  27. alternative approach. Namely, unblock the signals of interest while the
  28. program waits for I/O, and block said signals when the I/O is being
  29. serviced. You probably also want to set up the signal handlers to block the
  30. other relevant signals while they are executing, and to save/restore errno
  31. on handler entry/exit.
  32.  
  33. For example (some error checking omitted for clarity):
  34.  
  35.     /* set up signal mask */
  36.     .....
  37.     for (;;) {
  38.         do {
  39.             .....
  40.             /* unblock relevant signals */
  41.             (void)sigprocmask(SIG_UNBLOCK, &mask, NULL);
  42.  
  43.             nfds = select(MAXDESC, &rfdset, &wfdset, &efdset, timeout);
  44.             if (nfds < 0) error = errno;
  45.  
  46.             /* block relevant signals */
  47.             (void)sigprocmask(SIG_BLOCK, &mask, NULL);
  48.             .....
  49.         } while (nfds < 0 && error == EINTR);    /* spin on sigs */
  50.     
  51.         /* service I/O (or deal with nfds <= 0) */
  52.  
  53.         .......
  54.     }
  55. -- 
  56. Jim Robinson
  57. robinson@mdd.comm.mot.com
  58. {ubc-cs!van-bc,uunet}!mdivax1!robinson
  59.  
  60.