For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Help With signals.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help With signals.
Leif Ericksen

2006-01-10, 4:02 am

I am trying to set up a routine to trap signals so that I can pass it to
a sub that will act on the signals and it does not seem to be
functioning as I would think.

SAMPLE:
I will admit some of the code was taken from the camel book. :) I do
have use strict on.

my $name = "\n";
my $i = 0;

defined $Config{sig_name} || die "The Stupid System does not support
Signals?";
foreach $name(split(' ', $Config{sig_name}))
{
$Config::signo{$name} = $i;
$Config::signame[$i] = $name;
#print "$name:$i \t => SIG{'$name'} = \&sigcat\n";
$SIG{'$name'} = \&sigcat;
$i++;
}

Now I do have an while (1){}; setup so that I can test the break. Using
the above code I do not seem to enter my sub sigcat I do not see my
'special' signal catch message if I press ^C. HOWEVER, if I add the
following lines:

$SIG{'INT'} = \&sigcat;
$SIG{'HUP'} = \&sigcat;
$SIG{'STOP'} = \&sigcat;
$SIG{'ABRT'} = \&sigcat;
$SIG{'TERM'} = \&sigcat;

It does break out and lets me know that it received a HUP.

If I take the single quote ' off of the $name in the loop I get a
segmentation fault so I guess I need the quotes.

Can anybody tell me what is wrong with the loop and why it is not
working? Also if I uncomment the line:
#print "$name:$i \t => SIG{'$name'} = \&sigcat\n";
It appears to be running the correct set routine as in the 5 I have
shown above. I know I must have something simple but I just can not see
what it is...
--
Leif Ericksen <leife@dls.net>

Wiggins d'Anconia

2006-01-10, 4:02 am

Leif Ericksen wrote:
> I am trying to set up a routine to trap signals so that I can pass it to
> a sub that will act on the signals and it does not seem to be
> functioning as I would think.
>
> SAMPLE:
> I will admit some of the code was taken from the camel book. :) I do
> have use strict on.
>
> my $name = "\n";
> my $i = 0;
>
> defined $Config{sig_name} || die "The Stupid System does not support
> Signals?";
> foreach $name(split(' ', $Config{sig_name}))
> {
> $Config::signo{$name} = $i;
> $Config::signame[$i] = $name;
> #print "$name:$i \t => SIG{'$name'} = \&sigcat\n";
> $SIG{'$name'} = \&sigcat;


In the above line you are single quoting $name so that it is not
interpolated.

HTH,

http://danconia.org

> $i++;
> }
>
> Now I do have an while (1){}; setup so that I can test the break. Using
> the above code I do not seem to enter my sub sigcat I do not see my
> 'special' signal catch message if I press ^C. HOWEVER, if I add the
> following lines:
>
> $SIG{'INT'} = \&sigcat;
> $SIG{'HUP'} = \&sigcat;
> $SIG{'STOP'} = \&sigcat;
> $SIG{'ABRT'} = \&sigcat;
> $SIG{'TERM'} = \&sigcat;
>
> It does break out and lets me know that it received a HUP.
>
> If I take the single quote ' off of the $name in the loop I get a
> segmentation fault so I guess I need the quotes.
>
> Can anybody tell me what is wrong with the loop and why it is not
> working? Also if I uncomment the line:
> #print "$name:$i \t => SIG{'$name'} = \&sigcat\n";


Might want to examine the output more closely, $name should be being
printed as $name rather than INT, HUP, etc.

> It appears to be running the correct set routine as in the 5 I have
> shown above. I know I must have something simple but I just can not see
> what it is...

Leif Ericksen

2006-01-10, 4:02 am

Actually not quite what you thought on the output...
$ ./myt.pl
ZERO:0 => SIG{'ZERO'} = &sigcat
HUP:1 => SIG{'HUP'} = &sigcat
INT:2 => SIG{'INT'} = &sigcat
QUIT:3 => SIG{'QUIT'} = &sigcat
ILL:4 => SIG{'ILL'} = &sigcat
TRAP:5 => SIG{'TRAP'} = &sigcat
ABRT:6 => SIG{'ABRT'} = &sigcat
BUS:7 => SIG{'BUS'} = &sigcat
FPE:8 => SIG{'FPE'} = &sigcat
KILL:9 => SIG{'KILL'} = &sigcat

Also if I use the double quote as opposed to a single quote in:
SIG{'$name'} = \&sigcat;

I get a like result it does not work as desired and a complete lack of quotes gives me a
segmentation fault.


On Fri, 2006-01-06 at 15:37 -0700, Wiggins d'Anconia wrote:
> Leif Ericksen wrote:
>
> In the above line you are single quoting $name so that it is not
> interpolated.
>
> HTH,
>
> http://danconia.org
>
>
> Might want to examine the output more closely, $name should be being
> printed as $name rather than INT, HUP, etc.
>
>

--
Leif Ericksen <leife@dls.net>

Wiggins d'Anconia

2006-01-10, 4:02 am

Please bottom post...

Leif Ericksen wrote:
> Actually not quite what you thought on the output...
> $ ./myt.pl
> ZERO:0 => SIG{'ZERO'} = &sigcat
> HUP:1 => SIG{'HUP'} = &sigcat
> INT:2 => SIG{'INT'} = &sigcat
> QUIT:3 => SIG{'QUIT'} = &sigcat
> ILL:4 => SIG{'ILL'} = &sigcat
> TRAP:5 => SIG{'TRAP'} = &sigcat
> ABRT:6 => SIG{'ABRT'} = &sigcat
> BUS:7 => SIG{'BUS'} = &sigcat
> FPE:8 => SIG{'FPE'} = &sigcat
> KILL:9 => SIG{'KILL'} = &sigcat
>


Ah yep missed the double quotes.

> Also if I use the double quote as opposed to a single quote in:
> SIG{'$name'} = \&sigcat;


The above should be:

$SIG{$name} = \&sigcat;

$name can't be single quoted, and you have to call the SIG hash as a hash...

http://danconia.org
[color=darkred]
>
> I get a like result it does not work as desired and a complete lack of quotes gives me a
> segmentation fault.
>
>
> On Fri, 2006-01-06 at 15:37 -0700, Wiggins d'Anconia wrote:
>
Leif Ericksen

2006-01-10, 4:02 am

On Fri, 2006-01-06 at 15:58 -0700, Wiggins d'Anconia wrote:
> Please bottom post...
>
> Leif Ericksen wrote:
>
> Ah yep missed the double quotes.
>
>
> The above should be:
>
> $SIG{$name} = \&sigcat;


That causes...
../myt.pl
ZERO:0 => SIG{'ZERO'} = &sigcat
Segmentation fault

and if I just use
$SIG{'INT'} = /&sigcat; outside of the ;oop it seems to work just fine
so I am a little as to why this does not want to work. I even
tried creating a $temp and I assigned it like $temp = "'$name'"; and it
did not like that, unless I missed something when I did that. ;)

<SNIP>
--
Leif

John Doe

2006-01-10, 4:02 am

Leif Ericksen am Samstag, 7. Januar 2006 00.12:
> On Fri, 2006-01-06 at 15:58 -0700, Wiggins d'Anconia wrote:
>
> That causes...
> ./myt.pl
> ZERO:0 => SIG{'ZERO'} = &sigcat
> Segmentation fault


perldoc perlipc states:

"Another interesting signal to send is signal number zero.
This doesn't actually affect a child process, but instead
checks whether it's alive or has changed its UID."

I'm not a signal guru, but I think you can't assign a handler to the ZERO
signal. I also got a segfault.

Look at the following code, modified from yours.

BTW, your chances for answers are bigger if you provide code that can be run
by list members.

#!/usr/bin/perl
use strict;
use warnings;

use Config;

my $i = 0;
my (@signames, %signos);

defined $Config{sig_name}
or
die "The Stupid System does not support Signals?";

sub sigcat {print "sigcat called"; exit;};

foreach my $name (split(' ', $Config{sig_name})) {
next if $name eq 'ZERO';
$signos{$name} = $i;
$signames[$i] = $name;
print "$name:$i \t => SIG{$name} = \&sigcat\n";
$SIG{$name}=\&sigcat;
$i++;
}

{} while 1;



hth joe
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com