fork一个子进程,在子进程中做while(1)循环,在父进程中间隔几秒之后,posix_kill给子进程发送终止信号,
最后在ps 命令中还可以看到子进程的存在,只有父进程退出之后,子进程才一起消失
liunx 中使用查看命令是:
ps -aux | grep php
ps --ppid pid[父进程的进程号]
ps axjf | grep php
代码如下:
<?php
$fun7 = function () {
$pid = pcntl_fork();
switch ($pid) {
case -1:
exit("fork child process failed~\n");
case 0:
$childPid = posix_getpid();
while (true) {
echo "child process get pid = $childPid ~ \n";
sleep(3);
}
break;
default:
sleep(5);
posix_kill($pid, \SIGKILL);
for ($i = 0; $i < 10; $i++) {
$parentPid = posix_getpid();
echo "parent process get pid = $parentPid ~ \n";
sleep(2);
}
break;
}
echo "Done ~\n";
};
$fun7();
\pcntl_waitpid($pid)
父进程需要回收子进程资源
哦哦,懂了,谢谢哈,忘了调用了