Fork Example

S

Simon

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.
 
M

Michele Dondi

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Trouble with Net::Ping 9
cannot update with gem 2
ping - statistics 2
Problem with rubygems 0
Help with one-liner 12
IP address of webserver 2
Help with system() call and pipe 1
urllib on windows machines 4

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top