<?php $pid = posix_getpid(); $ppid = posix_getppid(); var_dump($pid); cli_set_process_title("myparent"); for($i = 1;$i<10;$i++){ $child = new Swoole\Process(function(){ cli_set_process_title("mychild"); echo "child ".getmypid().PHP_EOL; sleep(10); }); $child->start(); } while(true){ sleep(1); }
如果不进行回收,会产生僵尸进程
[root@localhost swoole]# php test.php int(31030) child 31031 child 31033 child 31034 child 31035 child 31032 child 31036 child 31037 child 31038 child 31039
子进程输入到父进程
<?php $pid = posix_getpid(); $ppid = posix_getppid(); var_dump($pid); cli_set_process_title("myparent"); $child = new Swoole\Process(function(){ cli_set_process_title("mychild"); while(true){ echo "child ".getmypid(); sleep(1); } },true); $child->start(); Swoole\Process::wait(false); // true是堵塞式的,读取子进程内容必须放在他前面,才能在被销毁之前拿到数据 //$child1 = new Swoole\Process(function(){ // cli_set_process_title("mychild1"); // echo "child1 ".getmypid().PHP_EOL; // while(true){ // sleep(1); // } // //}); //$child1->start(); while(true){ echo $child->read()." brady".PHP_EOL; sleep(1); }