一個簡單的Perl多線程腳本

 

===============================

#!/usr/bin/perl -w

use strict;

our $max_child = 20;

$|=1;

my $grp_pid = getpgrp();

foreach my $item (1..80){
   
    my $p_count = `ps -ef|grep ${grp_pid}|grep -v grep|wc -l`;
   
    while ($p_count >= $max_child) {
        print "達到最大進程數,休息一下!/n";
        $p_count = `ps -ef|grep ${grp_pid}|grep -v grep|wc -l`;
        chomp $p_count;
        print "/$p_count=${p_count}/n";
       
        last if ($p_count < $max_child);
       
        select undef,undef,undef,0.1;   #讓系統休息0.1秒
    }
   
    if (my $child_pid=fork()){
        print "開始啓動子進程:$child_pid/n";
    }
    else{
        print "掛起進程第:$item/n";
        print "當前子進程PID爲:$$;父進程PID爲:",getppid(),";進程組ID爲:",$grp_pid,"/n";
        sleep 60;
        exit 1;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章