alarm() bug in 4.2

Keith Muller muller at sdccsu3.UUCP
Wed Jul 11 17:46:43 AEST 1984


> Fortunately, there is an easy workaround; change the previous code
> to read:
> 
>	 for (...) {
>		 oldmask = sigblock(1<<SIGALRM);
>		 ...
>		 sigblock(oldmask);
>	 }

I should have looked closer as there is two bugs: (SIGALRM and the second
sigblock should be a sigsetmask)

	for (...) {
		oldmask = sigblock(1 << (SIGALRM - 1));
		....
		(void)sigsetmask(oldmask);
	}

Sigblock and sigsetmask together provide a nice way to encapsulate a
critical section to protect them from the delivery of asynchronous signals. 

Along the same grain, sigpause provides a neat way to set a signal mask
in an *atomic* manner:
	
	oldmask = sigblock(1 << (SIGALRM - 1));
	(void)setitimer(.....);
	sigpause(oldmask);

The above code is the only *sure* way to use sigpause with SIGALRM. As
you must block against delivery of the SIGALRM between the setitimer()
call and the sigpause(). If you don't the program will intermittantly
hang forever at the sigpause() (as the SIGALRM *can* be delivered *between*
the calls to setitime() and sigpause()).
Note that sigpause will restore the signal mask to oldmask after the
arrival of the SIGALRM.

		Keith Muller
		UCSD Computer Center



More information about the Comp.bugs.4bsd.ucb-fixes mailing list