home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ukma!wupost!zaphod.mps.ohio-state.edu!rpi!utcsri!robinson
- Newsgroups: comp.unix.programmer
- From: robinson@mdivax1.uucp (Jim Robinson)
- Subject: Re: Signal Handlers and malloc()
- Message-ID: <1992Nov14.002221.5672@mdivax1.uucp>
- Reply-To: robinson@mdd.comm.mot.com (Jim Robinson)
- Organization: Motorola - Mobile Data Division; Richmond, BC
- X-Newsreader: TIN [version 1.1 PL6]
- References: <BxIL1s.4py@flatlin.ka.sub.org>
- Date: 14 Nov 92 00:24:32 GMT
- Lines: 47
-
- Christoph Badura (bad@flatlin.ka.sub.org) wrote:
- >In <1992Nov9.064910.778@noose.ecn.purdue.edu> kudva@ecn.purdue.edu (Gautham K. Kudva) writes:
- >
- >> I've written a signal handler for the VTALRM signal. The signal
- >>handler calls another routine which does quite a bit of work. This program
- >>start crashing in an unpredictable fashion.
- >
- >You should rewrite your program so that it only sets a flag variable
- >in the signal handler and do the "quite a bit of work" out side the
- >signal handler in the main part of the program.
- >
- >This is the only way to make the program work reliably.
-
- If your program is structured as some sort of I/O loop, then there is an
- alternative approach. Namely, unblock the signals of interest while the
- program waits for I/O, and block said signals when the I/O is being
- serviced. You probably also want to set up the signal handlers to block the
- other relevant signals while they are executing, and to save/restore errno
- on handler entry/exit.
-
- For example (some error checking omitted for clarity):
-
- /* set up signal mask */
- .....
- for (;;) {
- do {
- .....
- /* unblock relevant signals */
- (void)sigprocmask(SIG_UNBLOCK, &mask, NULL);
-
- nfds = select(MAXDESC, &rfdset, &wfdset, &efdset, timeout);
- if (nfds < 0) error = errno;
-
- /* block relevant signals */
- (void)sigprocmask(SIG_BLOCK, &mask, NULL);
- .....
- } while (nfds < 0 && error == EINTR); /* spin on sigs */
-
- /* service I/O (or deal with nfds <= 0) */
-
- .......
- }
- --
- Jim Robinson
- robinson@mdd.comm.mot.com
- {ubc-cs!van-bc,uunet}!mdivax1!robinson
-
-