I have the following problem, that I have reconstructed in those two mini perl scripts. This is the main script:
#!/usr/bin/perl
$SIG{INT} = \&signal_handler_one;
open(my $pipe, "|-", "/home/pa/Desktop/POC2");
close $pipe;
sub signal_handler_one{
print "This is expected to print\n";
}
On the third line it opens a pipe to this script:
#!/usr/bin/perl
$SIG{INT} = \&signal_handler_two;
sleep(10);
sub signal_handler_two{
print "This isn't expected to print\n";
}
The problem is that when I start the first script and then send SIGINT to it while it is closing the pipe on line 4, the signal_handler_two is fired instead of signal_handler_one. Why does it behave like this? Is there any way around this (my goal is to get signal_handler_one to execute).
Edit: I originally sent the signal on the terminal using Ctrl+C, which causes "This isn't expected to print" to print. But when I send the signal using kill to the parent process from another terminal, it just ignores it.
Edit 2: I eventually solved it by not using open to get the pipe, but by manually forking, execing and then waiting on the child instead of just calling close. Now everything seems to work fine. It seems that that behaviour was specific to my environment, but if anyone could reproduce that same error, please let me know.