プロセスの制御

2555 ワード

`/*
  • マルチプロセス*/class processを開く//////最大プロセス数、最大30プロセスpublic$max=3;///linuxコマンドパスpublic$ps='/bin/ps';public$php='/usr/bin/php';public$wc='/usr/bin/wc';public$grep='/bin/grep';public$nohup='/usr/bin/nohup';////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////_number=3;//実績走行距離private$num;//プロセスid private$pid=array()function __construct($tmp=null){
    if( file_exists($tmp) ){
        $this->tmp = $tmp;
    }
    
    }function run($sh=',$avg){
    $this->sh = $sh;
    if( is_array( $avg ) ){
        $this->num = count($avg);
        foreach( $avg as $k=>$v ){
            $this->work($v);
        }
    }elseif( intval($avg) > 0 ){
        $avg = intval($avg);
        $this->num = $avg;
        for($i=0;$i<$avg;$i++){
            $this->work($i);
        }
    }else{
        exit('param error ...');
    }
    
    }//マルチプロセスを実行/$param shマルチプロセスを実行するファイル/@param num実際の需要プロセス数はデフォルトで最大プロセス数または最大プロセス数を超えることができますが、新しいプロセスfunction work($v){
    $this->pid[] = $v;
    //     0,      
    if($this->p_number <= 0){
        $this->p_number = $this->worker_processes();
    }
    //       
    $this->p_number = $this->p_number - 1;
    //      
    //echo 'start process ',$v,"
    "; $iswin = strpos(PHP_OS,"WIN"); if( $iswin === false ){ echo "$this->nohup $this->php $this->sh $v
    "; $out = popen("$this->nohup $this->php $this->sh $v > /dev/null 2> /dev/null &", "r"); pclose($out); }else{ echo "start /b php $this->sh $v
    "; $out = shell_exec("start /b php $this->sh $v"); }
    }//プロセス管理function worker_processes(){
    while ($this->p_number <= 0){
        //      
        $iswin = strpos(PHP_OS,"WIN");
        if( $iswin === false ){
            $line = shell_exec("$this->ps -ef | $this->grep $this->sh | $this->grep -v grep | $this->wc -l");
        }else{
            $output = shell_exec('wmic process where caption="php.exe" get processid');
            $output = explode("
    ",$output); $line = count( $output ) - 1; } $this->p_number = $this->max - $line; // 0 , if ($this->p_number <= 0){ echo "wait {$this->wait}s
    "; sleep( $this->wait );// 1 } } return $this->p_number;
    }

  • } `