Re: Dangers of intercepting SIGINT?
- From: "Tarkin" <Tarkin000@xxxxxxxxx>
- Date: 2 Feb 2007 15:35:10 -0800
On Feb 2, 3:21 pm, "Tarkin" <Tarkin...@xxxxxxxxx> wrote:
On Feb 2, 12:31 pm, "Loic Domaigne" <loic-...@xxxxxxx> wrote:
Hello,
In the process of writing a (POSIX) threaded program, I found I
wanted a clean way to exit the program, without screwing around with a
UI (yet!). I used signal(SIGINT,my_handler) to interceptSIGINT.
my_handler pthread_join()'s my threads, sends them a boolean condition
which makes them pthread_exit(), and the calls exit(0). AFAIK,
everything is cleaned up nice and tidy.
So, my question is: does the normal action(signal handler) for ^C
(SIGINT) do anything other than halt the process in it's tracks? i.e.
is there something from the default handler I should include in my
handler? Or am I already doing much more than the default ever does?
That's not the way you're supposed to deal with signal in multi-
threaded application. You might be lucky for now on your platform, but
there is no guarantee that this luck will hold forever.
Remember: to be on the safe side, you should only call async-signal-
safe function in signal handler. See:http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04...
Of course, this is too limitating for real world multi-threaded
programs. That why you should use the standard signal handling
framework found in the Examples section at:http://www.opengroup.org/onlinepubs/009695399/functions/pthread_sigma...
Cheers,
Loic.
O.k., let me rephrase the question: could folks suggest a
strategy to intercept ^C, so that I can cleanly shut down my
server process?
Here is a rough outline of what the server does:
1) Creates five threads, each of which has two
main conditions: ACTIVE and QUIT. If the thread
is ACTIVE, it already has an open socket and is
busy communicating. If the thread's QUIT condition
changes from 0, it must close it's socket and exit.
2) Goes through the socket/bind/listen/accept
phases of establishing a server communication
routine.
3) Passes accept'ed sockets to the next
non-ACTIVE thread, and goes back to
accept'ing.
The server process (main) prints various
and sundry messages to stdout. stdin is
left open, as it is desirable in development
to shutdown the code withoutusing
"kill -s 9 server_PID", and the easiest way
I know of to do that is to hit ^C.
I wanted to avoid writing terminal-dependant
code, or messing about with terminal libraries,
etc. if at all possible, so I looked into signal
handling. I made (the probably erroneous)
that *nix derivatives associate ^C from
stdin withSIGINT.
Comments, suggestions, URL's, criticism,
etc. welcomed.
TIA,
Tarkin
My very bad. Searching previous posts on SIGINT
in (this) groups archives has turned up plenty.
Thanks,
Tarkin
.
- References:
- Dangers of intercepting SIGINT?
- From: Tarkin
- Re: Dangers of intercepting SIGINT?
- From: Loic Domaigne
- Re: Dangers of intercepting SIGINT?
- From: Tarkin
- Dangers of intercepting SIGINT?
- Prev by Date: Gprof error
- Next by Date: Re: How to read out QString line-by-line?
- Previous by thread: Re: Dangers of intercepting SIGINT?
- Next by thread: Re: Dangers of intercepting SIGINT?
- Index(es):
Relevant Pages
|