查看 process.php 的内容

<?php
/**
 * 当前process.php 是主进程
 * 通过这个开启一个子进程
 *
 * 回调函数,第二参数: 启用此选项后,
 *  1:在子进程内输出内容将不是打印屏幕,而是写入到主进程管道。
 *  2:读取键盘输入将变为从管道中读取数据。默认为阻塞读取。
 */
$process = new swoole_process(function (swoole_process $process) {
    // 在终端执行 php http_server.php。在子进程执行http_server.php
    $process->exec("/usr/local/php/bin/php", [__DIR__.'/../03_httpServer/http_server.php']);
    echo "test stdin_stdout \n";
},  false);

$pid = $process->start();

echo "pid = {$pid}" . PHP_EOL;

// 等待回收结束运行的子进程。子进程结束必须要执行wait进行回收,否则子进程会变成僵尸进程
swoole_process::wait();

运行 process.php

php process.php

查看进程

 ps -auxf | grep php
root     13675  0.0  1.6 384660 31816 pts/0    S+   18:18   0:00  |       \_ php process.php
root     13676  0.0  2.9 500116 56248 pts/0    Sl+  18:18   0:00  |           \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php
root     13677  0.0  0.2 425880  4892 pts/0    S+   18:18   0:00  |               \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php
root     13679  0.0  0.2 411944  5016 pts/0    S+   18:18   0:00  |                   \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php
root     13680  0.0  0.2 411944  5016 pts/0    S+   18:18   0:00  |                   \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php
root     13681  0.0  0.2 411944  5016 pts/0    S+   18:18   0:00  |                   \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php
root     13682  0.0  0.2 411944  5016 pts/0    S+   18:18   0:00  |                   \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php
root     13683  0.0  0.2 411944  5016 pts/0    S+   18:18   0:00  |                   \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php
root     13684  0.0  0.2 411944  5016 pts/0    S+   18:18   0:00  |                   \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php
root     13685  0.0  0.2 411944  5016 pts/0    S+   18:18   0:00  |                   \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php
root     13686  0.0  0.2 411944  5016 pts/0    S+   18:18   0:00  |                   \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php

注意:创建子进程的内存占用是比较高的

root     13675  0.0(cpu)  1.6(mem) 
root     13676  0.0(cpu)  2.9(mem)

我们会发现一个主进程

php process.php

主进程下新建了一个子进程

Sl+  \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php

子进程下有去运行了一个子进程,且这个子进程下还有8个子进程

S+  \_ /usr/local/php/bin/php /var/www/swoole/07_process/../03_httpServer/http_server.php

s+,sl+代表了进程状态

    D    不可中断    Uninterruptible sleep (usually IO)
    R    正在运行,或在队列中的进程
    S    处于休眠状态
    T    停止或被追踪
    Z    僵尸进程
    W    进入内存交换(从内核2.6开始无效)
    X    死掉的进程

    <    高优先级
    N    低优先级
    L    有些页被锁进内存
    s    包含子进程
    +    位于后台的进程组
    l    多线程,克隆线程