What is the difference between ignoring a signal and pointing to it to do nothing in Perl?

If I tell Perl to explicitly ignore the signal, SIGINT has no effect:

$SIG{INT} = 'IGNORE'; my $count = 0; say $count++ and sleep 1 while 1; 

Then pressing Control-C obviously has no effect. If, on the other hand, I tell him to do nothing:

 $SIG{INT} = sub { }; my $count = 0; say $count++ and sleep 1 while 1; 

Then pressing the Control-C button has an effect! It wakes the program from the sleep () call and immediately increments the counter. What is the difference between ignoring a signal and telling it to do nothing?

In my program, I would like to run SIGINT code without breaking anything. I need something like:

 $SIG{INT} = sub { say "Caught SIGINT!"; return IGNORED; }; # runs without disrupting sleep 
+6
perl signals
source share
3 answers

What is the difference between ignoring a signal and pointing to it doing nothing?

The "do nothing" handler is still called when the signal is delivered, interrupting the sleep call in this case. Ignored signals, on the other hand, are simply discarded by the system without affecting the process.

See here for a C-specific but more complete reference.

+13
source share

Many system calls may be interrupted by a beep. You can usually check $! (aka errno ) to see if he has an EINTR and take steps to fail the failed system call again.

If it is important for you that certain sections of your code (in this case your sleep() call) are not interrupted, you can use POSIX::sigprocmask to block signals when you are in a critical section. Any signals received by the application will be queued until you unlock them, after which they will be delivered. I am sure that there is some funny semantics when several signals of a certain type are blocked (I think they can be combined into one signal).

Here's a nice primer from IBM on return functions and includes a small sample with sigprocmask .

+6
source share

Not sure why it doesn't work the way you expect, but usually when I try to do things like this, I also capture the TERM signal in addition to the INT signal.

+1
source share

All Articles