<?xml version="1.0" encoding="windows-1252"?>
<node id="1007745" title="anyevent + fork = problem" created="2012-12-07 09:08:38" updated="2012-12-07 09:08:38">
<type id="115">
perlquestion</type>
<author id="1007741">
hithim</author>
<data>
<field name="doctext">
  Hi dear monks , i try to make some kind of dhcp server with anyevent , here my test code :
&lt;code&gt;
#!/usr/bin/perl
use Data::Dumper;
use strict;
use warnings;
use EV;
use Fcntl;
use IO::Socket;
use AnyEvent;

$| = 1;
$0 = 'DHCP manager';
my $pid;
my %pids;
my @pids;
our %binds;
my $serv;
my $serv_ip = '10.10.10.10';
socket($serv, PF_INET, SOCK_DGRAM, getprotobyname('udp'));
fcntl($serv, F_SETFL, O_NONBLOCK);
setsockopt($serv, SOL_SOCKET, SO_REUSEADDR, 1);

my $new_child = sub {
	### create socket for ipc
	my $child;
	my $parent;
	socketpair($child, $parent, AF_UNIX, SOCK_DGRAM, PF_UNSPEC);
	fcntl($child, F_SETFL, O_NONBLOCK);
	fcntl($parent, F_SETFL, O_NONBLOCK);
	$child-&gt;autoflush(1);
	$parent-&gt;autoflush(1);
	
	if (!defined($pid = fork())) {# 
		print Dumper 'Cannot fork anymore';
		die "cannot fork: $!";
	
	} elsif ($pid) {				# parent
		$pids{$pid}{'sock'} = $parent;
		
		$pids{$pid}{'on_finish'} = AnyEvent-&gt;child (pid =&gt; $pid, cb =&gt; sub { ## when child die - start new one
			print Dumper 'CHILD Died'; 
			new_child();
		});
		
		$pids{$pid}{'ev'} = AnyEvent-&gt;io (fh =&gt; $parent, poll =&gt; 'r', cb =&gt; sub {
			my $buf;
			
			print Dumper $binds{'0019c632c637'}{'1'}{'001c238b8b02'};
		});
		
		push @pids , $pid;
		close $child;
		print Dumper "parent fork $pid";
	
	} else {						#child
		close $parent;
		undef %binds;
		$0 = 'DHCP worker';
		$| = 1;
		my $fh = $child;
		my $w = AnyEvent-&gt;io (fh =&gt; $fh, poll =&gt; 'r', cb =&gt; sub {
			my $buf;
			recv($fh, $buf, 500, 0);
			send ($fh , $buf, 0);
		
		});
		
		EV::loop();
		die;
	}
};

foreach (1..2){
	&amp;$new_child();
}

bind ($serv, sockaddr_in('67', inet_aton($serv_ip)));
listen($serv, SOMAXCONN);

my $server_hdl = AnyEvent-&gt;io (fh =&gt; $serv, poll =&gt; 'r', cb =&gt; sub {
	my $buf;
	my $fromaddr = recv($serv, $buf, 500, 0) or print Dumper $!;
	return if ($!);
	return if (length($buf) &lt; 236);
	my $hdl = shift @pids;
	my ($port, $addr) = unpack_sockaddr_in($fromaddr);
	$addr = inet_ntoa($addr);
	print Dumper "$$ Master get pocket from $addr";
	send ($pids{$hdl}{'sock'} , $addr.' '.$buf , 0);
	push @pids , $hdl;
});
print "Master listening\n";

$binds{'0019c632c637'}{'1'}{'001c238b8b02'}{'ip'} = '10.12.16.7';
$binds{'0019c632c637'}{'1'}{'001c238b8b02'}{'router'} = '10.12.16.1';
$binds{'0019c632c637'}{'1'}{'001c238b8b02'}{'mask'} = '255.255.255.0';

EV::loop();
&lt;/code&gt;

when i test this code - some times i get undef value on "print Dumper $binds{'0019c632c637'}{'1'}{'001c238b8b02'};" , can anyone help me ?
ps: when i create keys in hash %binds before fork - all is perfect</field>
</data>
</node>
