windows下.用不了pcntl.所以测试不了..

解决方案 »

  1.   

    for($i   =   0;   $i     <   $intNum;   $i++)   {   
            $pids[$i]   =   pcntl_fork();//   产生子进程,而且从当前行之下开试运行代码,而且不继承父进程的数据信息   
    }   这一段,$i=0时 fork了一个子进程,这时2个进程
    $i = 1时,两个进程又各fork了一个,这时4个进程
    $i = 2时,4个进程再,所以8个进程咯
      

  2.   

    回复you_kind
    是这样的,可是问题是父进程执行的任务,子进程会全权拷贝,我现在是想给子进程分配别的任务
      

  3.   

    <?php   
    //declare(ticks=1);   
    $bWaitFlag   =   FALSE;   //   是否等待进程结束   
    //$bWaitFlag   =   TRUE;   //   是否等待进程结束   
    $intNum   =   3;               //   进程总数   
    $pids   =   array();       //     进程PID数组   for($i   =   0;   $i     <   $intNum;   $i++)   {   
    $pids[$i]   =   pcntl_fork();//   产生子进程,而且从当前行之下开试运行代码,而且不继承父进程的数据信息   
    if($pids[$i]==0)
    break;
    }   if(!$pids[$i]){    //子进程任务
    while(1)   {   
    //   子进程进程代码段_Start   
    $str= " ";   
    sleep(5+$i);   
    for   ($j=0;$j   <$i;$j++)   {$str.= "* ";}   
    echo   "pid:".posix_getpid()."\t$i   -   >   "   .   time()   .   "   $str   \n ";   
    //   子进程进程代码段_End   
    }    
    }   
    else   //父进程等待子进程结束,虽然子进程结束不掉
    {   
    for($i   =   0;   $i     <   $intNum;   $i++)   {   
    pcntl_waitpid($pids[$i],   $status,   WUNTRACED);   
    echo   "wait   $i   -   >   "   .   time()   .   "\n ";   
    }   
    }   
    ?>   
    lz是要这样么?
      

  4.   

    回复you_kind
    8楼的代码是这几个进程在同时做同一件事情,如果做不同的事情该怎么办呢?
      

  5.   


    lz搞明白fork的道理,无非是内存和资源拷贝一份复制个进程,基本结构都是
    $pid = pcntl_fork();
    if ($pid == -1) {
         die('could not fork');
    } else if ($pid) {
         // we are the parent
         pcntl_wait($status); //Protect against Zombie children
    } else {
         // we are the child
    }
    多个子进程做一个事情还是多个事情就只是代码实现结构的问题咯
      

  6.   

    问you_kind//定义数组
    $arr = array('a','b','c','d','e','f','g','h');然后在让子进程一个一个的把数组中的元素打印出来?为什么会有重复打印的呢?while(1){
            //子进程进程代码段_Start
            $str="";
            sleep(5+$i);
            for($j = 0;$j < $i;$j++){
                //从$arr数组中取出一个元素进行打印
                $str = array_shift($arr);
            }
            echo "pid:".posix_getpid()."\t".$i."->".time()."$str\n";
            //子进程进程代码段_End
        }
      

  7.   

    //定义数组 
    $arr   =   array( 'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h '); 然后在让子进程一个一个的把数组中的元素打印出来?为什么会有重复打印的呢? 
    是想每个子进程取不同的数组元素?lz找点进程间通信方面的书看看,《Unix环境高级编程》一类
    具体做的话,共享内存或者管道都可以做,php里也都有相应模块和函数的
      

  8.   

    在php.net里找到了一个例子,谢谢大家的支持,尤其是you_kind
    <?php
    DEFINE(MAXPROCESS,25);
    $arr = array('a','b','c','d','e','f','g','h','i','j','k');
    $sub_arr = array_chunk($arr,3);
    $sub_num = count($sub_arr);
    for ($i=0;$i<$sub_num;$i++){
        $pid = pcntl_fork();
        if ($pid == -1) {
            die("could not fork");
        } elseif ($pid) {
            //echo "I'm the Parent $i\n";
            $execute++;
            if ($execute>=MAXPROCESS){
                pcntl_wait($status);
                $execute--;
            }
        } else {        echo "I am the child, $i pid = $pid \n";
            echo "Bye Bye from $i\n";
            print_r($sub_arr[$i]);
            sleep(rand(5,7));
            exit;
        }
    }
    ?>