CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Duplicate signal numbers in %Config



May 8, 2015, 11:51 AM

Post #1 of 4 (2047 views)
Duplicate signal numbers in %Config Can't Post

Greetings after a long absence from these pages. (What a difference it makes having a full-time job! Smile

I'm researching some aspects of signals for a Perl daemon I'm composing. So to help that research I wrote the following little program:

#!/usr/bin/perl -d -w 

use strict;
use Carp;
use Data::Dumper;

# The following use Config comes from:
# \
# specify-perl-signal-handler-using-the-number-not-the-name
use Config qw(%Config); # Dynamically creates the hash %Config

my $signame;

my @sig_names = split(/\s/, $Config{sig_name}); # array of signal names
my @sig_nums = split(/\s/, $Config{sig_num}); # and corresponding numbers

my $name_count = @sig_names;
my $num_count = @sig_nums;
printf("We have %d signal names and %d signal numbers\n",
$name_count, $num_count);

my %sig_hash; # Hash of signal numbers keyed by signal name
my $lc = 0; # Loop counter, of sorts

foreach $signame (@sig_names) # Build hash as descibed:
$sig_hash{$signame} = $sig_nums[$lc++]; # Key by sig-name, save sig number

printf "Printing from hash:\n";
foreach $signame (keys %SIG)
if (! defined($sig_hash{$signame})) {$sig_hash{$signame} = -1;}
printf("Signal[%02d]: %s\n", $sig_hash{$signame}, $signame);

printf "\nPrinting from array:s\n";
foreach my $sig_idx (0..$#sig_nums)
printf("Slot[%2d]: Signal[%02d]: %s\n",
$sig_idx, $sig_nums[$sig_idx], $sig_names[$sig_idx]);

foreach $signame (keys %SIG)
$SIG{$signame} = \&croaker; # Same function captures all signals
$SIG{HUP} = \&sig_catcher;

sub croaker
my $signal = $_[0];
printf("I caught signal <%s>; time to die\n", $signal);
croak "Signal <$signal> intercepted";

Note that I have seen the remarks in perldoc ( that tell me to use sig_name_init and sig_num_init instead of sig_name and sig_num. Actually, I did; it was a gluteal pain to remove the quotes and comments from arrays after the split. But the result was the same and I omitted that to keep the above code simpler.

The idea of the sleep() call is to give me a chance to kill -(signal number) and have it tell me which signal, by name, it intercepted. Along the way I made a startling discovery: There are a few signal numbers that are assigned more than one name! Here is an excerpt from the above's output:

Printing from array:s 
Slot[ 0]: Signal[00]: ZERO
Slot[ 1]: Signal[01]: HUP
Slot[ 2]: Signal[02]: INT
Slot[ 3]: Signal[03]: QUIT
Slot[ 4]: Signal[04]: ILL
Slot[ 5]: Signal[05]: TRAP
Slot[ 6]: Signal[06]: ABRT
Slot[ 7]: Signal[07]: EMT
Slot[ 8]: Signal[08]: FPE
Slot[ 9]: Signal[09]: KILL
Slot[10]: Signal[10]: BUS
Slot[11]: Signal[11]: SEGV
Slot[12]: Signal[12]: SYS
Slot[13]: Signal[13]: PIPE
Slot[14]: Signal[14]: ALRM
Slot[15]: Signal[15]: TERM
Slot[16]: Signal[16]: USR1
Slot[17]: Signal[17]: USR2
Slot[18]: Signal[18]: CHLD
Slot[19]: Signal[19]: PWR
Slot[20]: Signal[20]: WINCH
Slot[21]: Signal[21]: URG
Slot[22]: Signal[22]: IO
Slot[23]: Signal[23]: STOP
Slot[24]: Signal[24]: TSTP
Slot[25]: Signal[25]: CONT
Slot[26]: Signal[26]: TTIN
Slot[27]: Signal[27]: TTOU
Slot[28]: Signal[28]: VTALRM
Slot[29]: Signal[29]: PROF
Slot[30]: Signal[30]: XCPU
Slot[31]: Signal[31]: XFSZ
Slot[32]: Signal[32]: WAITING
Slot[33]: Signal[33]: LWP
Slot[34]: Signal[34]: FREEZE
Slot[35]: Signal[35]: THAW
Slot[36]: Signal[36]: CANCEL
Slot[37]: Signal[37]: LOST
Slot[38]: Signal[38]: XRES
Slot[39]: Signal[39]: RTMIN
Slot[40]: Signal[40]: NUM40
Slot[41]: Signal[41]: NUM41
Slot[42]: Signal[42]: NUM42
Slot[43]: Signal[43]: NUM43
Slot[44]: Signal[44]: NUM44
Slot[45]: Signal[45]: NUM45
Slot[46]: Signal[46]: RTMAX
Slot[47]: Signal[06]: IOT
Slot[48]: Signal[18]: CLD
Slot[49]: Signal[22]: POLL
Slot[50]: Signal[00]: 0

I don't know any ramifications of this. The worst that might happen is that I might announce a wrong signal name if I intercept, say signal 22: Is it POLL or is it IO? Hmmm... These sound related! Just as CLD and CHLD are likely intentionally the same signal.

Is this a historical artifact that will remain forever? Is there any reason for later versions (I'm stuck on 5.8 at my client) to cut out those last 4 slots?

Just researching this stuff. Angelic The perldoc "page" on Config is 124 pages in PDF.
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)

(This post was edited by rpaskudniak on May 8, 2015, 12:48 PM)

Veteran / Moderator

May 8, 2015, 1:01 PM

Post #2 of 4 (2042 views)
Re: [rpaskudniak] Duplicate signal numbers in %Config [In reply to] Can't Post

It's not real clear to me what your real goal is, but to my knowledge when you trap a signal (presumably you're using the sigtrap pragma), you trap it by name and then if needed you can lookup its number.

Do you have example code that traps by number rather than by name?

(This post was edited by FishMonger on May 8, 2015, 1:01 PM)


May 9, 2015, 1:19 PM

Post #3 of 4 (1963 views)
Re: [rpaskudniak] Duplicate signal numbers in %Config [In reply to] Can't Post

Hi, describes atleast the first three of the below signals to be synonyms. A few of them were introduced with later versions of POSIX.

Slot[47]: Signal[06]: IOT (synonym of SIGABRT) 
Slot[48]: Signal[18]: CLD (synonym of SIGCHLD)
Slot[49]: Signal[22]: POLL (synonym of SIGIO)
Slot[50]: Signal[00]: 0 (assumably a synonym of ZERO / exit(0))

Hope this helps,



May 9, 2015, 8:15 PM

Post #4 of 4 (1937 views)
Re: [Zhris] Duplicate signal numbers in %Config [In reply to] Can't Post

I could post it but it would be complicated (more honestly, cluttered Crazy ) and, in light of Zhris's response, now unnecessary. But I will shortly explain what I'm trying to do.

I had an inkling that the names associated with the duplicated numbers were related but I needed the opinion of someone more expert than myself to back that up. Fortunately, that is not difficult to find in this forum. Angelic The word synonym was just what I needed to clarify my understanding and solve my befuddlement.

It is fortunate that the signal function receives, as a parameter, the name of the signal. I had just wanted to associate it with a number so that, if exiting, my code would exit with that numeric code. For example, if I get signal "ABRT" (or IOT) I would exit(6).

Hmmm. only 3 short paragraphs? Who am I and what have I done with me? Wink
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives