CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
next element of array passed when using fork?

 



ajmcello
Novice

May 24, 2011, 5:20 PM

Post #1 of 2 (505 views)
next element of array passed when using fork? Can't Post

 
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:


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++;
}



(This post was edited by ajmcello on May 24, 2011, 5:21 PM)


FishMonger
Veteran / Moderator

May 24, 2011, 5:38 PM

Post #2 of 2 (499 views)
Re: [ajmcello] next element of array passed when using fork? [In reply to] Can't Post

http://forums.devshed.com/perl-programming-6/next-element-of-array-passed-when-using-fork-818645.html#post2645723

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives