function
pcntl_fork creates a new process
pcntl_waitpid waits or returns the child process status of fork
posix_getpid returns the current process id
posix_getppid obtains the parent process id
note
pcntl_fork is called once and returns two values; the child process gets 0, and the parent process gets the child process id.
View all running php processes
ps -ef |grep php
Instance
<?php
echo "Master process id=".posix_getpid().PHP_EOL;
$pid = pcntl_fork();
switch ($pid){
case -1:
die('Create failed');
case 0:
//Child
echo "Child process id = ".posix_getpid().PHP_EOL;
sleep(2);
echo "I will exit";
break;
default:
//等待子进程退出,后之心父进程的退出
if($exit_id = pcntl_waitpid($pid, $status, WUNTRACED)){
echo "Child({
$exit_id}) exited\n";
}
//Parent
echo "Parent process id = ".posix_getpid().PHP_EOL;
break;
}
sleep(18);