System signals
H. S. Teoh
hsteoh at quickfur.ath.cx
Sat Apr 28 14:48:12 PDT 2012
On Sat, Apr 28, 2012 at 02:08:06PM -0700, Sean Kelly wrote:
> On Apr 28, 2012, at 1:04 PM, "Andrea Fontana" <nospam at example.com>
> wrote:
>
> > How can i catch kill signal to gracefully shutdown my app?
>
> Gracefully? That's tough. I'd set a flag, then exit the signal
> handler. All threads should periodically check the flag and throw an
> Error if they see it. Or an Exception you're sure won't be caught.
> You really can't do much in a signal handler, so it is t safe to do
> something fancier there.
My favorite solution to this is to use the self-pipe trick:
http://evbergen.home.xs4all.nl/unix-signals.html
MUCH safer than setting a flag, or trying to do too much in a signal
handler, and can integrate seamlessly if your program is already using a
select loop.
Of course, if you're using threads then things may be a bit more
complicated. But still, moving the actual handling out of the signal
handler is a good thing; it lets you do more complex stuff (like
communicate with threads to stop, etc.).
Note, however, that SIGKILL is not catchable, ever.
T
--
MAS = Mana Ada Sistem?
More information about the Digitalmars-d
mailing list