php5.5 安装pcntl扩展

http://php.net/releases/

wget http://cn2.php.net/distributions/php-5.5.9.tar.bz2
tar xvf php-5.5.9.tar.bz2 -C .
cd ./php-5.5.9/ext/pcntl
/usr/bin/phpize5
./configure --with-php-config=/usr/bin/php-config5
make
sudo make install
ls /usr/lib/php5/20121212/
# sudo echo "extension=pcntl.so" >> /etc/php5/cli/php.ini
# sudo echo "extension=pcntl.so" >> /etc/php5/fpm/php.ini
php -m | grep pcntl

# php.ini  注释掉这行
disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl\
_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getp\
riority,pcntl_setpriority,


----------------------------------------------------------------------
Libraries have been installed in:
   /home/ubuntu/code/php/php-5.5.9/ext/pcntl/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

 

PHP模拟并发
https://blog.csdn.net/zhang_xinglong/article/details/16339867

 

测试程序

----------------------------------------------------

* pcntl.php

<?php

$b = function_exists("pcntl_fork");
var_dump($b);

declare(ticks=1);
//是否等待进程结束
$bWaitFlag = true;
//进程总数
$intNum = 10;
//进程PID数组
$pids = array();

echo "BEGIN\n";

for($i = 0; $i < $intNum; $i++) {
    //产生子进程,而且从当前行之下开试运行代码,而且不继承父进程的数据信息
    $pids[$i] = pcntl_fork();
    if( 0 === $pids[$i]) {
        //子进程进程代码段_Start
        $str = "";
        sleep($i);
        for ($j = 0; $j < $i; $j++) {
            $str .= "*";
        }
        echo "$i -> " . date('Y-m-d H:i:s', time()) . " $str \n";
        exit();
        //子进程进程代码段_End
    } else if (0 < $pids[$i]) {
        echo 'pid='.$pids[$i].PHP_EOL;
    }
}

if ($bWaitFlag) {
    for($i = 0; $i < $intNum; $i++) {
        pcntl_waitpid($pids[$i], $status, WUNTRACED);
        echo "wait $i(".$pids[$i].") -> " . date('Y-m-d H:i:s', time()) . "\n";
    }
}

echo ("END\n");

* test:

ubuntu@et-dev-mingzhanghui:~/code/test$ php pcntl.php
bool(true)
BEGIN
pid=5850
pid=5851
pid=5852
pid=5853
pid=5854
pid=5855
pid=5856
pid=5857
pid=5858
0 -> 2019-02-15 03:45:04  
pid=5859
wait 0(5850) -> 2019-02-15 03:45:04
1 -> 2019-02-15 03:45:05 * 
wait 1(5851) -> 2019-02-15 03:45:05
2 -> 2019-02-15 03:45:06 ** 
wait 2(5852) -> 2019-02-15 03:45:06
3 -> 2019-02-15 03:45:07 *** 
wait 3(5853) -> 2019-02-15 03:45:07
4 -> 2019-02-15 03:45:08 **** 
wait 4(5854) -> 2019-02-15 03:45:08
5 -> 2019-02-15 03:45:09 ***** 
wait 5(5855) -> 2019-02-15 03:45:09
6 -> 2019-02-15 03:45:10 ****** 
wait 6(5856) -> 2019-02-15 03:45:10
7 -> 2019-02-15 03:45:11 ******* 
wait 7(5857) -> 2019-02-15 03:45:11
8 -> 2019-02-15 03:45:12 ******** 
wait 8(5858) -> 2019-02-15 03:45:12
9 -> 2019-02-15 03:45:13 ********* 
wait 9(5859) -> 2019-02-15 03:45:13
END