develooper Front page | perl.perl5.porters | Postings from May 2008

[perl #54412] Mistake in perlipc doc, perl 5.10.0

Thread Previous
From:
Steve Peters via RT
Date:
May 18, 2008 18:50
Subject:
[perl #54412] Mistake in perlipc doc, perl 5.10.0
Message ID:
rt-3.6.HEAD-11257-1211161840-1500.54412-15-0@perl.org
> man perlipc states that:
> "Sending a signal to a negative process ID means that you send the
>    signal to
> the entire Unix process-group."
> and the example follows:
> "{
>         local $SIG{HUP} = 'IGNORE';
>         kill HUP => -$$;
>         # snazzy writing of: kill('HUP', -$$)
> }"
> 
> According to
> http://www.opengroup.org/onlinepubs/009695399/functions/kill.html
> "int kill(pid_t pid, int sig);
> [...]
> If pid is negative, but not -1, sig shall be sent to all processes
> (excluding an unspecified set of system processes) whose process group
>    ID is
> equal to the absolute value of pid,".
> 
> The problem is that variable $$ contains the current process pid,
> which is equal to "process group ID" ONLY for process group leader.
> 
> Example:
> -sh-3.00$ uname -a
> Linux somehost.net 2.6.16.33-xenU #2 SMP Fri Jan 11 00:07:17 UTC 2008
>    i686
> athlon
>  i386 GNU/Linux
> -sh-3.00$ cat p.pl
> #!/usr/bin/perl
> 
> print "Cur pid: $$\n";
> my $pgrpid = getpgrp();
> print "Cur PGID: $pgrpid\n";
> 
> print "Trying to kill cur pid-based pgrpid.\n";
> kill('HUP', -$$);
> sleep(1);
> 
> print "Trying to kill right pgrpid.\n";
> kill('HUP', -$pgrpid);
> 
> sleep(1);
> print "Successfully exited.\n";
> 
> 
> #p.pl is a process group leader now - it works
> -sh-3.00$ ./p.pl
> Cur pid: 1286
> Cur PGID: 1286
> Trying to kill cur pid-based pgrpid.
> Hangup
> 
> #p.pl NOT a leader now - the $$-based code does not work.
> -sh-3.00$ echo test|./p.pl
> Cur pid: 1288
> Cur PGID: 1287
> Trying to kill cur pid-based pgrpid.
> Trying to kill right pgrpid.
> Hangup
> 

I made a slight change to the script below.

#!/usr/bin/perl

print "Cur pid: $$\n";
my $pgrpid = getpgrp();
print "Cur PGID: $pgrpid\n";

print "Trying to kill cur pid-based pgrpid.\n";
kill('HUP', -$$) || warn $!;   #  <--warning
sleep(1);

print "Trying to kill right pgrpid.\n";
kill('HUP', -$pgrpid) || warn $!;     #  <--warning

sleep(1);
print "Successfully exited.\n";

...pops up a warning.

steve@picard:~$ echo test | ./p.pl
Cur pid: 23680
Cur PGID: 23679
Trying to kill cur pid-based pgrpid.
No such process at ./p.pl line 8.
Trying to kill right pgrpid.
Hangup


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About