Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Re: mixing multisockets and forks

by fxmakers (Friar)
on Mar 29, 2004 at 00:50 UTC ( #340463=note: print w/replies, xml ) Need Help??


in reply to Re: mixing multisockets and forks
in thread mixing multisockets and forks

Thanks for your reply!
I'm a little confused...
1st of all, yes, my "threads" could be renamed to children processes, sorry :)

The $SIG{CHLD} is called when a child process has finished, so we can retrieve its exit($result) using waitpid() and put the $result into the $status{$pid} hash.
use POSIX ":sys_wait_h"; $SIG{'CHLD'} = \&REAPER; # manage moribound children sub REAPER { my $pid = 0; while (($pid = waitpid(-1, &WNOHANG)) > 0) { $status{$pid} = $?; } $SIG{'CHLD'} = \&REAPER; }


The $threads decrement is done in the &timer_online, comparing the $status $pid with the client's pid list.


If you launch the 2nd portion of code (the fake "scan"), everything is running fine, the parent gets the finished child's $result using the &REAPER routine.

Here's a simple code:

use strict; use POSIX ":sys_wait_h"; $SIG{'CHLD'} = \&REAPER; sub REAPER { my $pid = 0; while (($pid = waitpid(-1, &WNOHANG)) > 0) { print STDOUT "$pid returned $?\n"; } $SIG{'CHLD'} = \&REAPER; } sub new_thread { my $result = 0; if (my $pid = fork()) { return($pid); } $result = 1; exit($result); } my $child = &new_thread(); print STDOUT "got a new child: $child\n"; for (;;) {} 1;


This way we can retrieve the $result.

If this works fine without the select() and vec() socket management, why are the fork()'s processes problematic when using them?


The simple complete code mixing them:
use strict; use POSIX ":sys_wait_h"; use IO::Socket::INET; $SIG{'CHLD'} = \&REAPER; sub REAPER { my $pid = 0; while (($pid = waitpid(-1, &WNOHANG)) > 0) { print STDOUT "$pid returned $?\n"; } $SIG{'CHLD'} = \&REAPER; } sub new_thread { my $result = 0; if (my $pid = fork()) { return($pid); } sleep(3); $result = 1; exit($result); } my %socklist = (); # hash table of sockets my $bit = ''; # connect to the IRC server my $socket = &open_socket("tcp", "irc.creatixnet.com", 6667, 3); sub open_socket { my ($proto, $host, $port, $timeout) = (shift, shift, shift, shift); my $socket = IO::Socket::INET->new( Proto => $proto, PeerAddr => $host, PeerPort => $port, Timeout => $timeout ); if (!$socket) { return(0); } return($socket); } if (!$socket) { die("can't connect"); } &add_socket($socket, "IRC"); sub add_socket { my ($socket, $type) = (shift, shift); $socket->autoflush(1); $socklist{$socket}{'sock'} = $socket; $socklist{$socket}{'type'} = $type; $socklist{$socket}{'fileno'} = $socket->fileno(); vec($bit, $socklist{$socket}{'fileno'}, 1) = 1; return(0); } print $socket "USER zxf sdf sdf sdf\n"; print $socket "NICK sdfsdfg\n"; print $socket "JOIN #fx\n"; my $child = &new_thread(); print STDOUT "got a new child: $child\n"; sub loop_socket { my $rin = $bit; $rin =~ /[^\0]/ || next; my $idx = select($rin, undef, undef, 1); my $buffer = ""; $idx || next; $idx > 0 || die($!); my %list = %socklist; while ($idx && (my ($socket, $value) = each(%list))) { $socket = $value->{'sock'}; if (vec($rin, $value->{'fileno'}, 1)) { $buffer = <$socket>; print + STDOUT "IRC: $buffer"; } } return(0); } for (;;) { &loop_socket(); } 1;

When children exits: No children at test.txt line 82

:(
Cheers

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://340463]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-11-30 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?