Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146851
  • 博文数量: 52
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 490
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-05 12:05
文章分类

全部博文(52)

文章存档

2013年(1)

2010年(3)

2009年(6)

2008年(25)

2007年(17)

我的朋友

分类:

2010-04-22 13:15:06

#!/usr/bin/perl -w

use POSIX ":sys_wait_h";
our $num = 3000;
our $fnum = 0;
our @childs;

$SIG{CHLD} = \&REAPER;

#if parent get CHLD exit signal, we do $fnum--, 
#so while loop will fork another child
sub REAPER {
        $fnum--;
        #$fnum = 0;
        print "fnum--\n";
        my $pid;  while (($pid = waitpid(-1, WNOHANG)) > 0) {

                print "$pid die\n";
        }

}

while($num>0){
    if($fnum<1000){
        my $pid = fork();
        if($pid ==0 ){
                #child
                sleep(int(rand(5)));
                #sleep(1);
                print "this is a child $num\n";
                exit(0);
        }
        else{
                push(@childs,$pid);
                print "fnum++\n";
                $fnum++;
                $num--;
                print "fork child $pid\n";
                print "fnum is $fnum\n";
        }
     }
}

foreach(@childs){
        waitpid($_,0);
        $fnum=0;
}

阅读(740) | 评论(0) | 转发(0) |
0

上一篇:Perl面向对象

下一篇:learning python logging

给主人留下些什么吧!~~