mock up test is below.. it works but i am wondering if there is anything i am doing wrong or better ways to do things like this...
[user1@myserver program]$ cat m_fork.pl
#!/usr/bin/perl
use warnings;
use strict;
for ( 1 .. 10 ) {
print "how are you\n";
}
system("/home/user1/program/test_fork.pl");
for ( 1 .. 10 ) {
print "I love the results\n";
}
[user1@myserver program]$
[user1@myserver program]$
[user1@myserver program]$ cat ./test_fork.pl
#!/usr/bin/perl
use warnings;
use strict;
my $child = fork ();
die "can't fork: $!" unless defined $child;
print "PID=$$\n";
if ( $child > 0 ) { #parent process
print "Parent process: PID=$$, child=$child\n";
system("/home/user1/program/echo1.pl");
} else {
my $ppid = getppid();
print "child process: PID=$$, parent=$ppid\n";
system("/home/user1/program/echo2.pl");
}
[user1@myserver program]$
[user1@myserver program]$
[user1@myserver program]$
[user1@myserver program]$ cat ./echo1.pl
#!/usr/bin/perl
use warnings;
use strict;
for ( 1 .. 5 ) {
print "this is echo1....\n";
sleep 10;
}
[user1@myserver program]$
[user1@myserver program]$
[user1@myserver program]$ cat ./echo2.pl
#!/usr/bin/perl
use warnings;
use strict;
for ( 1 .. 5 ) {
print "this is echo2....\n";
sleep 10;
}
[user1@myserver program]$ ./m_fork.pl
how are you
how are you
how are you
how are you
how are you
how are you
how are you
how are you
how are you
how are you
PID=2076
child process: PID=2076, parent=2075
PID=2075
Parent process: PID=2075, child=2076
this is echo1....
this is echo2....
this is echo1....
this is echo2....
this is echo1....
this is echo2....
this is echo1....
this is echo2....
this is echo1....
this is echo2....
I love the results
I love the results
I love the results
I love the results
I love the results
I love the results
I love the results
I love the results
I love the results
I love the results