I have an array with 5 elements. With each fork, I want the next element to pass to it and then exit. Here's what I've got so far. Could someone please help? :)
Here's what I get:
child 0 k: test1
child 0 k: test2
child 0 k: test3
child 0 k: test4
child 0 k: test5
child 1 k: test1
child 1 k: test2
child 1 k: test3
child 1 k: test4
child 1 k: test5
child 2 k: test1
child 2 k: test2
child 2 k: test3
child 2 k: test4
child 2 k: test5
child 3 k: test1
child 3 k: test2
child 3 k: test3
child 3 k: test4
child 3 k: test5
child 4 k: test1
child 4 k: test2
child 4 k: test3
child 4 k: test4
child 4 k: test5
Here's what I'd like it to say:
child 0 k: test1
child 1 k: test2
child 2 k: test3
child 3 k: test4
child 4 k: test5
Thanks in advance!
code:
#!/usr/bin/perl
@array = qw(test1 test2 test3 test4 test5);
$x=0;
$num = 5;
for ( 1 .. $num ) {
my $pid = fork();
if ($pid) {
push( @childs, $pid );
}
elsif ( $pid == 0 ) {
print "parent\n";
sleep 5;
exit(0);
}
else {
die "couldnt fork: $!\n";
}
}
foreach (@childs) {
foreach $k (@array) {
print "child $x k: $k\n";
next;
}
$x++;
}