doylebobs has asked for the wisdom of the Perl Monks concerning the following question:
I'm pretty much a newbie to Perl and am trying to write a script that forks. The child will use Net::RawIP and opens a packet capture descriptor and reads an unspecified number of packets. After 10 seconds, the parent, signals the child to terminate using a KILL signal. The problem is that a <defunct> process for the child remains, at least as far as ps -eaf is concerned. After one minute, the script restarts again. I need to be able to kill the child process after a specified length of time. Any suggestions on the best way to do this?
#!/usr/bin/perl use warnings; use strict; use Net::RawIP; use POSIX; daemonize(); while (1) { my $pid = fork(); print "Process number of script is $$\n"; unless (defined ($pid)) { die "Resources unavailable to fork\n"; } elsif ($pid == 0) { my $a = new Net::RawIP; my $p = $a->pcapinit("eth0","udp port 68",1500,30); my $f = ["User-defined message"]; loop $p,10,\&process_pkt,$f; } elsif ($pid) { for (my $i=0; $i <= 10; $i++ ) { sleep 1; print "in parent...$i seconds have elapsed...\n"; } print "killing child process $pid"; kill 9, $pid; sleep 60; } } # end of while loop sub process_pkt { print "packet captured\n"; my $raw_pkt = $_[2]; my $raw_data = unpack "H*", $raw_pkt; print $raw_data,"\n"; } sub daemonize { my $pid = fork(); exit if $pid; setsid(); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Best way to kill a child process
by jwkrahn (Abbot) on Sep 21, 2011 at 05:44 UTC | |
Re: Best way to kill a child process
by zentara (Archbishop) on Sep 21, 2011 at 10:26 UTC | |
Re: Best way to kill a child process
by MidLifeXis (Monsignor) on Sep 21, 2011 at 13:08 UTC | |
Re: Best way to kill a child process
by Marshall (Canon) on Sep 21, 2011 at 13:32 UTC | |
by Eliya (Vicar) on Sep 21, 2011 at 13:58 UTC | |
by Marshall (Canon) on Sep 21, 2011 at 15:31 UTC | |
by Eliya (Vicar) on Sep 21, 2011 at 18:41 UTC | |
by Marshall (Canon) on Sep 22, 2011 at 00:50 UTC | |
| |
Re: Best way to kill a child process
by doylebobs (Novice) on Sep 22, 2011 at 06:00 UTC |
Back to
Seekers of Perl Wisdom