Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 30 Mar 2003 21:49:10 -0800 (PST)
From:      Brian Buchanan <bwb@holo.org>
To:        =?ISO-8859-1?Q?Mikko_Ty=F6l=E4j=E4rvi?= <mbsd@pacbell.net>
Cc:        Sean Hamilton <sh@bel.bc.ca>
Subject:   Re: wait()/alarm() race condition
Message-ID:  <20030330214750.A65240-100000@thought.holo.org>
In-Reply-To: <20030330191611.J1122@atlas.home>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 30 Mar 2003, [ISO-8859-1] Mikko Työläjärvi wrote:

> On Sun, 30 Mar 2003, Sean Hamilton wrote:
>
> > Dan Nelson wrote:
> > | 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.
> >
> > Er, I think you've missed my problem. Or I'm not getting your solution.
> >
> > I'm concerned about this order of events:
> >
> > - alarm()
> > - wait() returns successfully
> > - if (alarmed...) [false]
> > - SIGALRM is delivered, alarmed = true
> > - loop
> > - wait() waits indefinitely
> >
> > This is incredibly unlikely to ever happen, but it's irritating me somewhat
> > that the code isn't airtight. Bad design. Surely there is some atomic means
> > of setting a timeout on a system call.
>
> My stock solution to this kind of problem is to turn those pesky
> signals into I/O and use an old fashioned select() loop to handle
> them; create a pipe(2), let signal handlers write one-byte "messages"
> (the signal number) into the pipe and then use select() to dequeue the
> events (signals) from the pipe.
>
> Select() has a timeout parameter you can play with to your hearts
> content, and provided you don't overflow the pipe, no events will
> get lost.  You'd have to install a hander for SIGCHLD, of course.

Or how about kqueue(2) with EVFILT_SIGNAL.  That would seem to be a more
elegant solution.  No signal handlers or alarm() required.

-Brian

-- 
Brian Buchanan, CISSP                                         bwb@holo.org
--------------------------------------------------------------------------
FreeBSD - The Power to Serve!                       http://www.freebsd.org



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