IPC + fork()s + messy output 
Author Message
 IPC + fork()s + messy output

Greetings!

fork()s (configurable) which perform a traceroute for each $host and stores

---------------------------->8-----------------------------
$max_childs = "35"; # maximum number of childs

{
        if (!defined($pid = open($fh[$j], "-|")))
        {
                die "Couldn't fork() : $!\n";
        }
        elsif ($pid == "0")
        {
                print STDOUT trace($host);
                exit;
        }
        else
        {
                ++$n;
                vec($rin, fileno($fh[$j]), 1) = 1;
                $found = select($rout=$rin, undef, undef, 0);
                foreach $j (0..$j)
                {
                        if (vec($rout, fileno($fh[$j]), 1))
                        {
                        sysread($fh[$j], $final[$i++], length($fh[$j]));
                        }
                }
                if ($n == $max_childs)
                {
                        wait;
                        --$n;
                }
                ++$j;                          
        }
Quote:
}

$SIG{CHLD} = sub { wait; };

---------------------------------->8-----------------------
My problems are the following :

still working and probably they don't return any output. Actually, when my


buffered I wouldn't have experienced any problems with STDOUT, my output is
awful.

Any solution to these ?

D.

PS. $n is the current number of childs



Sun, 29 Aug 2004 01:11:58 GMT  
 IPC + fork()s + messy output

Quote:

> Greetings!

> of fork()s (configurable) which perform a traceroute for each $host

> into my main loop :

Hmm, very ugly.  I would do it something like:

use IO::Select;
use IPC::Open2;
my $max_children = 35;

my $sel = IO::Select->new;
my %hosts;

sub newchild {

   open( \local(*NULL), "<", "/dev/null" )
      or die "Couldn't open /dev/null! $!";;

   open2( my($reader), "<&NULL", "traceroute", $host );

   $sel->add($reader);
   $hosts{$reader} = $host;

Quote:
}


my %data;
while( $sel->count ) { foreach my $fh ($sel->can_read) {
   local *_ = \ $data{$hosts{$fh}}; $_ = "" if !defined;
   my $cnt = sysread( $fh, $_, 4096, length ) and next;
   defined($cnt) or die "sysread failed: $!";
   $sel->remove($fh); close $fh;
   my $host = delete $hosts{$fh};
   my $data = delete $data{$host};

   print $host, ":\n", "=" x 60, "\n", $data, "\n";

   wait;

Quote:
} }

__END__
[untested]

--
print reverse( ",rekcah", " lreP", " rehtona", " tsuJ" )."\n";



Sun, 29 Aug 2004 02:09:23 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. newline problem with messy output - v simple problem

2. newbie - messy output from system command?

3. Using open() for IPC -- can't fork due to bad file descriptor

4. IPC::Shareable performance with forked processes.

5. dbi, fork and ipc-shareable - having problems

6. multiple forks and IPC

7. Problem wit ActiveState on NT IPC(pipes) and fork

8. Fork, IPC:Open2, Windows?

9. Multithreaded/Forked/IPC Perl

10. IPC::open2 duplicates output

11. Exporter.pm usage is uncessarily messy

12. messy DOS, winDoze, and the great bit bucket in the sky

 

 
Powered by phpBB® Forum Software