Current location: Hot Scripts Forums » Programming Languages » Perl » next element of array passed when using fork?


next element of array passed when using fork?

Reply
  #1 (permalink)  
Old 05-24-11, 07:28 PM
ajmcello ajmcello is offline
New Member
 
Join Date: May 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
next element of array passed when using fork?

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++;
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help reading multi-dimensional array l3rady JavaScript 3 06-11-10 04:10 AM
Get array element from this string... phpdoctor PHP 1 02-13-08 09:19 PM
Make A New Array From An Old Array (Excluding 1 Array Element) w2n PHP 14 08-17-07 03:24 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM


All times are GMT -5. The time now is 04:56 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.