# test_proc.pl
# test multi process
# create by lianming: 2009-08-12
use warnings;
my $pid = fork();
print "Error in fork: $!";
exit 1;
}
print "Child: My pid = $$\n";
sleep(5);
print "Child: end\n";
exit 0;
} else {
print "Parent My pid = $$, and my child's pid = $pid\n";
sleep(5);
print "Parent: end\n";
}
Parent My pid = 19480, and my child's pid = 19481
Child: end
Parent: end
# test_proc_1.pl
# test multi process
# create by lianming: 2009-08-12
use warnings;
for (my $i = 0; $i < 10; $i ++) {
## == fork a new process ==
my $pid = fork();
print "Error in fork: $!";
exit 1;
}
print "Child $i : My pid = $$\n";
sleep(5);
print "Child $i : end\n";
exit 0;
}
sleep(1);
}
Child 1 : My pid = 20500
Child 2 : My pid = 20501
Child 3 : My pid = 20502
Child 4 : My pid = 20503
Child 0 : end
Child 5 : My pid = 20506
Child 1 : end
Child 6 : My pid = 20507
Child 2 : end
Child 7 : My pid = 20508
Child 3 : end
Child 8 : My pid = 20509
Child 4 : end
Child 9 : My pid = 20510
Child 5 : end
# Child 6 : end
Child 7 : end
Child 8 : end
Child 9 : end
root 20532 0.0 0.0 0 0 pts/2 Z+ 21:46 0:00 \_ [perl]
root 20535 0.0 0.0 0 0 pts/2 Z+ 21:46 0:00 \_ [perl]
root 20536 0.0 0.0 0 0 pts/2 Z+ 21:46 0:00 \_ [perl]
root 20539 0.0 0.0 0 0 pts/2 Z+ 21:46 0:00 \_ [perl]
root 20541 0.0 0.0 8460 720 pts/2 S+ 21:46 0:00 \_ perl test_proc_1.pl
root 20543 0.0 0.0 8460 720 pts/2 S+ 21:46 0:00 \_ perl test_proc_1.pl
root 20545 0.0 0.0 8460 720 pts/2 S+ 21:46 0:00 \_ perl test_proc_1.pl
root 20546 0.0 0.0 8460 720 pts/2 S+ 21:46 0:00 \_ perl test_proc_1.pl
root 20548 0.0 0.0 8460 720 pts/2 S+ 21:46 0:00 \_ perl test_proc_1.pl
if ($procid == 0) {
# == child process ==
print ("this line is printed first\n");
exit(0);
} else {
# == parent process ==
waitpid ($procid, 0);
print ("this line is printed last\n");
}
# test_proc_2.pl
# test multi process
# create by lianming: 2009-08-12
use warnings;
use POSIX ":sys_wait_h";
my $zombies = 0;
$SIG{CHLD} = sub { $zombies++ };
for (my $i = 0; $i < 10; $i ++) {
## == fork a new process ==
my $pid = fork();
print "Error in fork: $!";
exit 1;
}
print "Child $i : My pid = $$\n";
sleep(5);
print "Child $i : end\n";
exit 0;
}
if ($zombies > 0) {
while (($collect = waitpid(-1, WNOHANG)) > 0) {
$zombies --;
}
}
sleep(1);
}
Child 1 : My pid = 21553
Child 2 : My pid = 21554
Child 3 : My pid = 21555
Child 4 : My pid = 21556
Child 0 : end
Child 5 : My pid = 21558
Child 1 : end
Child 6 : My pid = 21570
Child 2 : end
Child 7 : My pid = 21572
Child 3 : end
Child 8 : My pid = 21574
Child 4 : end
Child 9 : My pid = 21575
Child 5 : end
# Child 6 : end
Child 7 : end
Child 8 : end
Child 9 : end
root 21558 0.0 0.0 8280 1168 pts/2 S+ 22:07 0:00 \_ perl test_proc_2.pl
root 21570 0.0 0.0 8280 1168 pts/2 S+ 22:07 0:00 \_ perl test_proc_2.pl
root 21572 0.0 0.0 8280 1168 pts/2 S+ 22:07 0:00 \_ perl test_proc_2.pl
root 21574 0.0 0.0 8280 1168 pts/2 S+ 22:07 0:00 \_ perl test_proc_2.pl
root 21575 0.0 0.0 8280 1168 pts/2 S+ 22:07 0:00 \_ perl test_proc_2.pl
# test_proc_3.pl
# test multi process
# create by lianming: 2009-08-12
use warnings;
use POSIX ":sys_wait_h";
my $num_proc = 0;
my $num_collect = 0;
$SIG{CHLD} = sub { $num_proc-- };
my $pid = fork();
print "Error in fork: $!";
exit 1;
}
print "Child $i : My pid = $$\n";
sleep(5);
print "Child $i : end\n";
exit 0;
}
if (($i-$num_proc-$num_collect) > 0) {
while (($collect = waitpid(-1, WNOHANG)) > 0) {
$num_collect ++;
}
}
do {
sleep(1);
} until ($num_proc < 3);
}
Child 1 : My pid = 22642
Child 2 : My pid = 22643
Child 0 : end
Child 3 : My pid = 22645
Child 1 : end
Child 4 : My pid = 22647
Child 2 : end
Child 5 : My pid = 22658
Child 3 : end
Child 6 : My pid = 22660
Child 4 : end
Child 7 : My pid = 22661
Child 5 : end
Child 8 : My pid = 22663
Child 6 : end
Child 9 : My pid = 22664
Child 7 : end
# Child 8 : end
Child 9 : end
root 22660 0.0 0.0 0 0 pts/2 Z+ 22:29 0:00 \_ [perl]
root 22661 0.0 0.0 8116 1168 pts/2 S+ 22:29 0:00 \_ perl test_proc_3.pl
root 22663 0.0 0.0 8116 1168 pts/2 S+ 22:29 0:00 \_ perl test_proc_3.pl
root 22664 0.0 0.0 8116 1168 pts/2 S+ 22:29 0:00 \_ perl test_proc_3.pl