Hi!
I'm not sure to understand how waitpid works, as my script always dies returning "No children at test.pl lineXX".
I'm connecting to some servers using a multisocket and vec/select and forking some subs at the same time.
These childs exit with a $result I'd like to store.
Code follows:
use strict;
use POSIX ":sys_wait_h";
use IO::Socket::INET;
$SIG{'CHLD'} = \&reaper;
sub reaper {
my $child;
while (($child = waitpid(-1, &WNOHANG)) > 0) {
my $exit = $? >> 8;
print STDOUT "$child returned $exit\n";
}
$SIG{'CHLD'} = \&reaper;
}
my %socklist = ();
my $bit = '';
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 sdxfsdfg\n";
print $socket "JOIN #fx\n";
sub new_child {
my $result = 0;
my $pid;
if ($pid = fork()) {
print STDOUT"new child $pid\n";
return($pid);
} elsif (defined($pid)) {
print STDOUT "child code here\n";
sleep(2);
$result = 2;
exit($result);
}
}
my $child = &new_child();
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;
Please give me a hand, I'd really appreciate it :)