|Think about Loose Coupling|
Interesting behaviour with signals and threadsby shulam (Novice)
|on Oct 06, 2019 at 13:23 UTC ( #11107096=perlquestion: print w/replies, xml )||Need Help??|
shulam has asked for the wisdom of the Perl Monks concerning the following question:
I have been debugging a program which uses signals and threads this weekend.
The program goes somewhat like this:
1. Fork a child which sends a "child ready" signal (USR2) to parent after it has done processing a thing.
2. Start a thread in parent, which does things in background while waiting for the child to be ready.
3. Handle the "child ready" signal in parent which indicates the child is ready to proceed to doing another thing.
4. Repeat this until receiving an INT or TERM signal.
Now this works mostly fine. A behaviour, which I don't understand, starts happening when I add a signal handler for INT and TERM, in addition to the "child ready" signal, which is USR2.
I have simulated the program with a code snippet. It starts a child which is constantly sending TERM and USR2 signal to parent.
(TERM signal normally comes from init process, here it comes from the child)
Parent has handlers for both signals AND has a thread which has handlers for both those signals.
I expected the thread signal handlers to never be called, since I don't call threads->kill anywhere.
Here is the code, it outputs a message every time a signal handler is called:
I get output like this:
The thread signal handlers are not called if I send only one type of signal from child process.
If I comment out one of kill-commands from child code, so that I send only USR2 or TERM, I don't get the thread signal handler output.
It doesn't make a difference if I change the TERM signal to be USR1
Can someone explain why this happens?
Perl version information:
Running on Debian Linux Stretch
Back to Seekers of Perl Wisdom