I have two shared libraries linked to my test application. Both of the libraries have signal handlers for SIGINT
.
Is it valid to have multiple signal handlers for same signal? Which order the handlers will execute when I generate a SIGINT
signal?
we can handle multiple signal with single signal handler but but its is not possible to have multiple signal handler for same signal.
if u try to run this code u will find that the last assigned signal handler is set for that signal. i think it is not possible to have a multiple signal handler for same signal.
Only one signal handler can be installed per signal. Only the latest installed handler will be active.
Shabaz hit the nail on the head. However, if you're looking for something all your libraries could use (provided you have access to the source code), you could do something along the following lines:
Or, you could use this yourself, and after loading each library, get the handler and later call add_handler. Something along the lines of:
Just some thoughts, Anthony
As said by others, only one signal handler can be set, which is the last one. You would then have to manage calling two functions yourself. The
sigaction
function can return the previously installed signal handler which you can call yourself.Something like this (untested code):
As you could see in the man page for sigaction, the new signal handler replaces the old one and the old one is returned.
If you have two unused signals (say
SIGUSR1
andSIGUSR2
), assign those signals the two signal handlers forSIGINT
. Then you may write your own Signal Handler forSIGINT
and from that, you may raise the needed unused signal as per you want.