php-pcntlHow to use pcntl_waitpid in PHP?
PCNTL_WAITPID is a PHP function used to wait for a child process to terminate. It takes two parameters, the first being the PID of the process to wait for, and the second being a variable to store the status of the child process.
<?php
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// parent
$status = null;
pcntl_waitpid($pid, $status);
} else {
// child
exit();
}
The output of the above code will be nothing, as the child process exits immediately.
Code explanation
pcntl_fork()
- This function creates a child process.pcntl_waitpid($pid, $status)
- This function waits for the child process to terminate, and stores the status of the child process in the second parameter.exit()
- This function exits the child process.
Helpful links
More of Php Pcntl
- How to use PCNTL alarm in PHP?
- How to use pcntl_wexitstatus in PHP?
- How to use pcntl_wifexited in PHP?
- How to install PCNTL for PHP in Debian?
- How to use pcntl_signal in PHP?
- How to kill a process with PHP PCNTL?
- How to check if PCNTL is enabled in PHP?
- How to prevent zombie processes with the PCNTL_FORK function in PHP?
- How to use shared memory with the PCNTL_FORK function in PHP?
See more codes...