将“use POSIX ":sys_wait_h";”去掉,还有在主进程中打印子进程中的变量是不对的,主进程不知道子进程的状况,除非你用pipe将变量值传给主进程, ################################################## #!/usr/bin/perl #use POSIX ":sys_wait_h"; my $a=10; for($i=1;$i<=3;$i++){ my $pid=fork(); if (!defined($pid)) { print "Error in fork: $!"; exit 1; } if ($pid == 0 ) { if($i==1){ sleep(9); $b1=$a+1; print "$b1\t"; exit 0;} elsif($i==2){ sleep(5); $b2=$a+10; print "$b2\t"; exit 0;} else{ sleep(3); $b3=$a+100; print "$b3\t"; exit 0; } } } print "~~~~~~~~~~split~~~~~~~~~~~\n"; while (($collect = waitpid(-1, WNOHANG)) > 0) { 1; } ##################### 输出 ########## bsd2# perl test.pl ~~~~~~~~~~split~~~~~~~~~~~ 110 20 11 bsd2#
perl多进程主进程等子进程结束
最新推荐文章于 2021-07-31 10:18:17 发布