Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

signal handling

by debugger (Novice)
on Feb 15, 2010 at 15:09 UTC ( #823288=perlquestion: print w/replies, xml ) Need Help??
debugger has asked for the wisdom of the Perl Monks concerning the following question:

The following code is not able to trap a signal (when i press ctrl-c). It doesnt call the subroutine sig_handler.

1. File a.pl call b.pl which runs in an infinite loop, as soon as i run the command=>
perl a.pl
I get "In B" as output and it continues in infinite loop.
However when i press ctrl-c , "In A" is printed and program exits.
The statement "Sig trapped" is not printed at all.
a.pl :

$SIG{INT}=\&sig_handler; system("perl b.pl"); print "In A\n"; sub sig_handler { printf("Sig trapped.\n"); }

b.pl:
print "In B\n"; for(;;) { }
I m using linux and perlv5.8.8.

. Please suggest a solution that will work on windows as well as linux.
The code is working on windows using Active perl v5.8.8.
I mean i get the message "Sig trapped" on windows.

Replies are listed 'Best First'.
Re: signal handling
by crashtest (Curate) on Feb 15, 2010 at 21:11 UTC

    I played around a little with this on my Windows machine (Vista) at work, and my Linux machine at home. I think there's just a difference in how the two OS interpret what "Ctrl-C" means.

    In the Linux case, hitting Ctrl-C sends the INT signal to the child process you forked using system. That signal is handled by the default handler, which means the child process terminates. Control passes back to the parent normally. The signal handler in the parent doesn't come into play. However, if you checked $? or the system call return value, you'd see that the child had exited with a signal.

    In the Windows case, things start out the same way. The signal is sent to the child (I checked), but it it appears that if there's no signal handler in the child, the signal is sent to the parent instead. Whether this is a Perl or a Windows thing, I can't tell. In any case, I think the Linux behavior is more sane.

    If you want to handle both OS, why don't you just install a signal handler in the child? That'll behave the same in both cases. Alternatively, if you're OK with letting the child terminate, but want to know what happened when control returns to the parent, just check $? for the details:

    system("perl b.pl"); my $exit_code = $? >> 8; my $exit_signal = $? & 127;
    (see perlvar)

      Thanks for the information.
      The exit signal helped out.
Re: signal handling
by Anonymous Monk on Feb 15, 2010 at 15:12 UTC
    b.pl doesnt send signals to parent
      But the code works fine on windows. I mean i get the "Sig trapped" message on windows using active perl.
        Windows doesn't implement unix signals, so it's no surprise that some differences exists between its two signals (Ctrl-C and Ctrl-Break) and unix signals.
        accident of implementation, perlport says Don't count on signals or %SIG for anything.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://823288]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2019-03-27 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (126 votes). Check out past polls.

    Notices?