This is the first time I need to do fork , so Im reading and trying to learn how to do it.
Im reading and processing the log lines of my dhcp sever wich are coming really fast.
For each line I receive I need to perform a snmpget querie to the ips Im grabing.
The problem is that each querie takes more than a second to return a value so it is delaying all the script so I had to do fork.
I have this first version working but I would like have your opinion in order to improve it or make it run in a more secure way.
#!/usr/bin/perl -w use strict; use File::Tail; use Cwd qw(); my $path = Cwd::cwd(); $SIG{'CHLD'} = "wait_for_child_to_die"; my $community='public'; my $snmp_fver = '.1.3.6.1.4.1.2700.1.1.8.0'; my $file = File::Tail->new( name =>'/var/log/messages', interval => 1, maxinterval => 1, # resetafter=> 5, ); while (defined(my $line=$file->read)) { #print $line,"\n"; if ($line =~ /^(.*) dns02cor dhcpd: DHCPACK on ([0-9\. +]+) to ([[:xdigit:]:]+).* ([0-9\.]+)/) { unless (my $pid = fork) { die "Couldn't fork " unless defined $pid; write_register($1,$2,$3); exit; } } } sub write_register { my ($date,$client_ip,$client_imsi) = @_; $output=qx(snmpwalk -v2c -t1 -c $community $client_ip $snmp_fver 2>&1 +); chomp($output); if( $output eq "Timeout: No Response from $client_ip" ) { return; } else{ my @result=split(/:/,$output); if ($result[3]){ $fver=$result[3]; $fver=~s/ //g; $fver=~s/\n//g; }else{ exit; } } if($bsid){ system "sed -i '/$client_imsi/d' $path/leases_list.txt +"; system 'echo "'.$date.','.$client_ip.','.$client_imsi.','. $fv +er.'" >> '.$path.'/leases_list.txt'; } } sub wait_for_child_to_die { wait; } 1;

I must admint this is a mix of multiple scripts I founded on the web.
I hope to hear your opinion in case is there is something I should be aware or change.
I promess to read and learn about forking , wait , and related topics.
So far , it is not creating more than 8 process and no zombies also so Im very happy.
Regards,
Leandro.

In reply to Im I forking properly ? by leostereo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.