Hi Perl Monks,
I tried using signals on Windows XP. I was thinking to use signal as a way for IPC to have the target process do something upon a signal and keep alive. But I found that whenever a process receives a signal from another process, it exits right away without **doing anything (i.e., it is not processing the code in the handler)** and **not keeping alive**
I verified that the process only reacts on the signals having been associated with handlers.
My questions:
1. How can I make the code in the handler to be executed?
2. Is there a way to keep the process alive after receiving the signal?
Here is the example, run this script in one command line window first.
#!c:\perl\bin\perl.exe
#Assign signal handlers
$SIG{'NUM12'} = 'handler';
$SIG{'CHLD'} = 'handler';
print "List current SIG handlers...\n";
while ( my ($key, $value) = each(%SIG) ) {
print "$key => $value\n" if ($value ne '');
}
print "My PID is $$. I go sleep and wait for signals and ...\n";
while(1) {
sleep;
}
sub handler{
local ($sig) = @_;
# Problem: This line never printed.
print "I am doing something for SIG $sig....\n";
}
Then, run this in another command line window:
perl -e "kill CLD, <the PID displayed in the first window>"
What happened was--whenever the first process received the signal, it exit right away without printing any message from the handler.
Any idea?
Thanks
PerlSearcher
Update:
Thanks all for the replies.
On Win32, I found it only reacted on the signals I associated with handlers. That means the signal number ideed got sent over to the other process by the KILL function.
Also, I tested all the signals in the %SIG by associating all the signals to a handler. Here is what I found--when a Win32 Perl process gets a signal (through emulation), all of them caused the process to exit without executing the code in handler, except for the following signals, in which cases, the process receiving the signals just do nothing (neither executing the code in handler nor exiting).
--BREAK
--INT
--QUIT
--TERM
In summary, it is not a good idea to use signal on Win32 for IPC, if you have other approaches to achieve the goal.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.