alarm questions

Chris Torek chris at umcp-cs.UUCP
Sun Jul 27 00:21:27 AEST 1986


In article <9250003 at acf4.UUCP> tihor at acf4.UUCP (Stephen Tihor) writes:
>... a friend wrote a little infinite sleep loop with a signal handler
>that displays the signal number.  His testing with kill implies that
>the ALARM signal does interupt the sleep but doesn't invoke the handler.

This is indeed the case.  To see why, you must first know that
there is no sleep `system call'.  There once was such a call, I
understand; but it was replaced with alarm().  alarm() is more
general than sleep(), for it can alert you some time in the future,
whether or not you are doing anything.  To sleep, all you need do
is ask to be alerted, then do nothing:

	/*
	 * Simplified `sleep' routine.
	 */
	sleep(seconds)
		int seconds;
	{
		int wake_me_up();

		signal(SIGALRM, wake_me_up);
		alarm(seconds);		/* set the alarm clock */
		sigpause(0);		/* then take a nap */
	}

	wake_me_up()
	{

		/* do nothing: sigpause returns after any signal occurs. */
	}

sleep() is actually horridly complex, to account for possible
pending alarms, other signal handlers, and so forth; and due to
signal vagaries, only the versions in 4.2 and 4.3 BSD can be written
reliably (and as I recall, the distributed 4.2 version was buggy
anyway!).  But the basic idea is that simple: set the alarm and
wait for it to go off.  If you use `kill -ALRM', sleep thinks that
alarm signal is its own, and returns.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix mailing list