php-pcntlHow to use pcntl_fork in PHP?
PCNTL stands for Process Control, and is a library of functions for PHP that allow you to create and manage processes. The pcntl_fork()
function is used to create a new process from the current process.
<?php
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// parent process
pcntl_wait($status); //Protect against Zombie children
} else {
// child process
echo "I'm a child process!";
exit();
}
Output example
I'm a child process!
Code explanation
pcntl_fork()
: creates a new process from the current processif ($pid == -1)
: checks if the process was created successfullyelse if ($pid)
: executes the parent processelse
: executes the child processpcntl_wait($status)
: protects against Zombie children
Helpful links
More of Php Pcntl
- How to use PCNTL alarm in PHP?
- How to use pcntl_wifexited in PHP?
- How to use pcntl_signal in PHP?
- How to use pcntl_waitpid in PHP?
- How to get the process ID with PHP PCNTL?
- How to prevent zombie processes with the PCNTL_FORK function in PHP?
- How to use pcntl_wexitstatus in PHP?
- How to use pcntl_wait in PHP?
- How to install PCNTL for PHP in Debian?
- How to check if PCNTL is enabled in PHP?
See more codes...