Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 30 Mar 2003 20:38:56 -0600
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Sean Hamilton <sh@bel.bc.ca>
Cc:        hackers@freebsd.org
Subject:   Re: wait()/alarm() race condition
Message-ID:  <20030331023856.GL74971@dan.emsphone.com>
In-Reply-To: <001101c2f71d$8d9e4fb0$0300000a@slugabed.org>
References:  <001101c2f71d$8d9e4fb0$0300000a@slugabed.org>

next in thread | previous in thread | raw e-mail | index | archive | help
In the last episode (Mar 30), Sean Hamilton said:
> [asked in comp.unix.programmer without much luck]
> 
> I have a loop which calls wait(), and I want another function to be
> called as close to once per minute as possible. Pseudo code:
[snip]
> My concern is there is a small possibility that the alarm signal is
> delivered after the if() but before the wait. So it is possible that
> this wait takes several minutes or longer.
> 
> Moving the "if (alarmed)" above the wait() makes no difference, of course.
> 
> Is there a better way of doing something like this? Ideally wait() has a
> timeout parameter, but, no such luck.

Just make sure your signal handler has the SA_RESTART flag unset
(either via siginterrupt() if the handler was installed with signal(),
or directly if the signal was installed with sigaction() ), and the
signal will interrupt the wait() call.

-- 
	Dan Nelson
	dnelson@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030331023856.GL74971>