The following example forks off 41 children using pipes. The children sleep for some time, then write data (this should be similar to your children). After having spawned off all the children, the parent is going to read all the pipes. It could very well be that the children are done in a different order than the parent is going to inspect them, but since you have to wait till the last one is finished, it doesn't matter (well, it would if there would be huge amounts of data involved). After reading all the data, we reap the children using wait, although that shouldn't be necessary since we didn't install a signal handler.
#!/usr/bin/perl use strict; use warnings 'all'; my @kids; foreach my $count (0 .. 40) { my $pid = open $kids [$count] => "-|"; die "Failed to fork: $!" unless defined $pid; unless ($pid) { # Child. sleep rand 60; # Sleep for some random time. print "$$: ", int rand 0x7FFFFFFF, "\n"; exit; } } foreach my $fh (@kids) { my @lines = <$fh>; print "Got: @lines"; } 1 while -1 != wait;
Abigail
In reply to Re: How do I get the child's data?
by Abigail-II
in thread How do I get the child's data?
by pepik_knize
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |