Fork Example

Discussion in 'Perl Misc' started by Simon, Jul 5, 2007.

  1. Simon

    Simon Guest

    Hi guys!

    I have the following fork script:

    ================================

    my $child_pid = fork;
    if ($child_pid) {
    predator($child_pid);
    print "$child_pid";
    }
    elsif (defined($child_pid)) {
    back();
    exit;
    }
    else
    {
    warn "fork failed: $!\n";
    }




    sub predator {
    print `ping -n 30 predator`;
    }


    sub back {
    print `ping -n 30 backup`;
    }

    ===================================

    Output is as follows..

    C:\TEMP>hi.pl

    Pinging predator [192.168.2.4] with 32 bytes of data:

    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.4: bytes=32 time<10ms TTL=128
    -122
    Pinging backup [192.168.2.2] with 32 bytes of data:

    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128
    Reply from 192.168.2.2: bytes=32 time<10ms TTL=128

    C:\TEMP>

    ================================================

    What Id like to know is:

    1) There is no output on the screen until each process is complete, so I get
    a quick flash of the above as output.
    Is there any way to kick off the child and parent simultaneously in a new
    command window, and see each line eg.each ping reply line by line?

    2) Im trying to understand how I can utilize the benefits of fork. Can you
    kick off more than 2 processes eg.child and parent, so that you could do
    this, for say, 20 systems?
    Eg. so you would have 20 instances of this script each pinging a different
    system, each in its own command window?

    Any help appreciated.
    Simon, Jul 5, 2007
    #1
    1. Advertising

  2. On Thu, 05 Jul 2007 04:21:51 GMT, "Simon" <> wrote:

    >Is there any way to kick off the child and parent simultaneously in a new
    >command window, and see each line eg.each ping reply line by line?


    Fork a command window (terminal emulator, I suppose) and execute the
    ping in it.

    >2) Im trying to understand how I can utilize the benefits of fork. Can you
    >kick off more than 2 processes eg.child and parent, so that you could do
    >this, for say, 20 systems?


    Yes, just as many as you want. (Up to ulimit or complete hog of your
    system.) Did you try?

    >Eg. so you would have 20 instances of this script each pinging a different
    >system, each in its own command window?


    I would call this a nightmare, but if you like it...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jul 5, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CwK

    fork

    CwK, Dec 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    920
    Misha Gale
    Jan 4, 2004
  2. Josh Denny

    fork in perl 5.8.3 on windows

    Josh Denny, Mar 2, 2004, in forum: Perl
    Replies:
    2
    Views:
    6,634
    Jim Gibson
    Mar 2, 2004
  3. Patrick
    Replies:
    1
    Views:
    509
  4. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    560
    Eric Snow
    Jan 8, 2009
  5. Sam Roberts
    Replies:
    15
    Views:
    276
    Sam Roberts
    Feb 7, 2005
Loading...

Share This Page