alarm code

Andrew Klossner andrew at orca.UUCP
Mon Mar 11 17:29:34 AEST 1985


> Although this solution to the problem may be sound, this method of implementing
> it is, er... dumb (to say the least).  The following implementation is much
> easier to understand, simpler and (Oh wow!) portable:
> 
> 
> 		int (*foo)();
> 		int pause();
> 			...
> 		{
> 			...
> 			/* Point at the pause() call */
> 			foo = pause;
> 			/* Set up alarm */
> 			alarm(1);
> 			/* Actually do pause (well, maybe) */
> 			(*foo)();
> 			/* Proceed merrily along */
> 		}
> 
> 		int null()
> 		{}	/* Do nothing function */
> 
> 		alarm_signal_handler()
> 		{
> 			foo = null;
> 		}

It also doesn't solve the problem.  Consider this sequence of events:
alarm(1) is called; now we begin executing the code for (*foo)() as
follows: fetch the contents of foo() and do the subroutine-call
instruction.  Before the first instruction of the pause() subroutine is
executed, the signal goes off.  Away we go to alarm_signal_handler
which changes the value of "foo".  Then back to the pause routine,
where we do the Unix call for pause, which waits forever.

The portable way to solve this problem is to use setjmp/longjmp.

  -- Andrew Klossner   (decvax!tektronix!orca!andrew)       [UUCP]
                       (orca!andrew.tektronix at csnet-relay)  [ARPA]



More information about the Comp.unix.wizards mailing list