Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

anyevent + fork = problem

by hithim (Initiate)
on Dec 07, 2012 at 14:08 UTC ( #1007745=perlquestion: print w/replies, xml ) Need Help??
hithim has asked for the wisdom of the Perl Monks concerning the following question:

Hi dear monks , i try to make some kind of dhcp server with anyevent , here my test code :
#!/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 = ''; 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->autoflush(1); $parent->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->child (pid => $pid, cb => + sub { ## when child die - start new one print Dumper 'CHILD Died'; new_child(); }); $pids{$pid}{'ev'} = AnyEvent->io (fh => $parent, poll => 'r', +cb => 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->io (fh => $fh, poll => 'r', cb => sub { my $buf; recv($fh, $buf, 500, 0); send ($fh , $buf, 0); }); EV::loop(); die; } }; foreach (1..2){ &$new_child(); } bind ($serv, sockaddr_in('67', inet_aton($serv_ip))); listen($serv, SOMAXCONN); my $server_hdl = AnyEvent->io (fh => $serv, poll => 'r', cb => sub { my $buf; my $fromaddr = recv($serv, $buf, 500, 0) or print Dumper $!; return if ($!); return if (length($buf) < 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'} = ''; $binds{'0019c632c637'}{'1'}{'001c238b8b02'}{'router'} = ''; $binds{'0019c632c637'}{'1'}{'001c238b8b02'}{'mask'} = ''; EV::loop();
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

Replies are listed 'Best First'.
Re: anyevent + fork = problem
by roboticus (Chancellor) on Dec 07, 2012 at 15:08 UTC


    I don't understand why you're using fork. Since there's no a whole lot of logic to DHCP, it's not like you have large tasks you need to fork off to keep your server available. Don't you just need to hit a hash and send a reply? That should be quick enough to just handle as it comes without splitting off another process.

    Similarly, if you *want* to use fork, then I don't understand why you want to use AnyEvent.

    I've not used AnyEvent before, so there might simply be something I don't understand, though.


    When your only tool is a hammer, all problems look like your thumb.

Re: anyevent + fork = problem
by blue_cowdawg (Monsignor) on Dec 07, 2012 at 15:21 UTC
        , i try to make some kind of dhcp server with anyevent ,

    Why? There are decent DHCP servers already out there for just about every flavor/variety of OS ever written... I'd be willing to bet there's even one for Android! Reinventing the wheel....

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: anyevent + fork = problem
by hithim (Initiate) on Dec 07, 2012 at 15:14 UTC
    serializ/deserialize packet is take some time and for highload server i need to use all cores of processor so i decide to use fork.


      Really? How long is it taking to serialize and deserialize? I wouldn't expect it to take enough time to worry about.


      When your only tool is a hammer, all problems look like your thumb.

Re: anyevent + fork = problem
by hithim (Initiate) on Dec 07, 2012 at 22:26 UTC
    guys , i really appreciate your comments , but i need solve exactly this issue.
      :) Kinda the purpose behind AnyEvent is so you don't fork :) You could use Proc::Background, stat a file in your anyevent stuff, stat a file, when file changes, get data from file
Re: anyevent + fork = problem
by ftumsh (Scribe) on Dec 19, 2012 at 13:38 UTC
    Have a look at fork_call in AnyEvent::Util, it may help

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1007745]
Approved by ww
[Cosmic37]: FANN also has LGPL license which I like and its supposed to be quite a capable library from what I read
[corenth]: Cosmic, that sounds interesting. What is FANN (I could search it if I weren't so lazy about it)?
[Cosmic37]: how big was @stuff corenth?
[Cosmic37]: Fast Artificial Neural Network (FANN) is cross-platform open source programming library for developing multilayer feedforward Artificial Neural Networks
[corenth]: @stuff was pretty big. I think it grew to about 8000.
[corenth]: each element was several three + element references
[corenth]: That FANN thing sounds like something I
[Cosmic37]: When I say I like LGPL what I mean is that I like the GPL aspect. I am a fan of Rishard Stallman and the Free Software Foundation but I wish I could be more virtuous... self-flagellates
[corenth]: . . . I'd like to play with

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-02-20 18:21 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.