php[world] in Washington, D.C.

pcntl_fork

(PHP 4 >= 4.1.0, PHP 5)

pcntl_forkForks the currently running process

Description

int pcntl_fork ( void )

The pcntl_fork() function creates a child process that differs from the parent process only in its PID and PPID. Please see your system's fork(2) man page for specific details as to how fork works on your system.

Return Values

On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and a PHP error is raised.

Examples

Example #1 pcntl_fork() example

<?php

$pid 
pcntl_fork();
if (
$pid == -1) {
     die(
'could not fork');
} else if (
$pid) {
     
// we are the parent
     
pcntl_wait($status); //Protect against Zombie children
} else {
     
// we are the child
}

?>

See Also

add a note add a note

User Contributed Notes 18 notes

up
12
Tony
4 years ago
If you want to execute some code after your php page has been returned to the user. Try something like this -

<?php
function index()
{
        function
shutdown() {
           
posix_kill(posix_getpid(), SIGHUP);
        }

       
// Do some initial processing

       
echo("Hello World");

       
// Switch over to daemon mode.

       
if ($pid = pcntl_fork())
            return;    
// Parent

       
ob_end_clean(); // Discard the output buffer and close

       
fclose(STDIN);  // Close all of the standard
       
fclose(STDOUT); // file descriptors as we
       
fclose(STDERR); // are running as a daemon.

       
register_shutdown_function('shutdown');

        if (
posix_setsid() < 0)
            return;

        if (
$pid = pcntl_fork())
            return;    
// Parent

        // Now running as a daemon. This process will even survive
        // an apachectl stop.

       
sleep(10);

       
$fp = fopen("/tmp/sdf123", "w");
       
fprintf($fp, "PID = %s\n", posix_getpid());
       
fclose($fp);

        return;
}
?>
up
4
sean dot kelly at mediatile dot com
3 years ago
"Fatal Error" has always been the bane of my world because there is no way to capture and handle the condition in PHP. My team builds almost everything in PHP in order to leverage our core library of code, so it was of the essence to find a solution for this problem of scripts bombing unrecoverably and us never knowing about it.

One of our background automation systems creates a "task queue" of sorts and for each task in the queue, a PHP module is include()ed to handle the task. Sometimes however a poorly behaved module will nuke with a Fatal Error and take out the parent script with it.

I decided to try to use pcntl_fork() to isolate the task module from the parent code, and it seems to work: a Fatal Error generated within the module makes the child task bomb, and the waiting parent can simply catch the return code from the child and track/alert us to the problem as needed.

Naturally something similar could be done if I wanted to simply exec() the module and check the output, but then I would not have the benefit of the stateful environment that the parent script has so carefully prepared. This allows me to keep the child process within the context of the parent's running environment and not suffer the consequences of Fatal Errors stopping the task queue from continuing to process.

Here is fork_n_wait.php for your amusement:

<?php

if (! function_exists('pcntl_fork')) die('PCNTL functions not available on this PHP installation');

for (
$x = 1; $x < 5; $x++) {
   switch (
$pid = pcntl_fork()) {
      case -
1:
        
// @fail
        
die('Fork failed');
         break;

      case
0:
        
// @child: Include() misbehaving code here
        
print "FORK: Child #{$x} preparing to nuke...\n";
        
generate_fatal_error(); // Undefined function
        
break;

      default:
        
// @parent
        
print "FORK: Parent, letting the child run amok...\n";
        
pcntl_waitpid($pid, $status);
         break;
   }
}

print
"Done! :^)\n\n";
?>

Which outputs:
php -q fork_n_wait.php
FORK: Child #1 preparing to nuke...
PHP Fatal error:  Call to undefined function generate_fatal_error() in ~fork_n_wait.php on line 16
FORK: Parent, letting the child run amok...
FORK: Child #2 preparing to nuke...
PHP Fatal error:  Call to undefined function generate_fatal_error() in ~/fork_n_wait.php on line 16
FORK: Parent, letting the child run amok...
FORK: Child #3 preparing to nuke...
PHP Fatal error:  Call to undefined function generate_fatal_error() in ~/fork_n_wait.php on line 16
FORK: Parent, letting the child run amok...
FORK: Child #4 preparing to nuke...
PHP Fatal error:  Call to undefined function generate_fatal_error() in ~/fork_n_wait.php on line 16
FORK: Parent, letting the child run amok...
Done! :^)
up
2
iulian
4 years ago
When using fork to run multiple children processes on a single job queue using mysql, I used mysql_affected_rows() to prevent collisions between workers:

First I find a "free" job:
SELECT job_id FROM queue WHERE status="free"

Then I update the queue:
UPDATE queue SET worker_id={$worker_id} WHERE job_id={$job_id}

Then I see if the row was changed

<?php
if(mysql_affected_rows() == 0)
{
//the row hasn't changed, so it must mean that another worker has claimed the job, so I go back to the "find a free job" query
}
else
{
//do the job
}
?>
up
2
duerra at yahoo dot com
4 years ago
Using pcntl_fork() can be a little tricky in some situations.  For fast jobs, a child can finish processing before the parent process has executed some code related to the launching of the process.  The parent can receive a signal before it's ready to handle the child process' status.  To handle this scenario, I add an id to a "queue" of processes in the signal handler that need to be cleaned up if the parent process is not yet ready to handle them. 

I am including a stripped down version of a job daemon that should get a person on the right track.

<?php
declare(ticks=1);
//A very basic job daemon that you can extend to your needs.
class JobDaemon{

    public
$maxProcesses = 25;
    protected
$jobsStarted = 0;
    protected
$currentJobs = array();
    protected
$signalQueue=array();  
    protected
$parentPID;
  
    public function
__construct(){
        echo
"constructed \n";
       
$this->parentPID = getmypid();
       
pcntl_signal(SIGCHLD, array($this, "childSignalHandler"));
    }
  
   
/**
    * Run the Daemon
    */
   
public function run(){
        echo
"Running \n";
        for(
$i=0; $i<10000; $i++){
           
$jobID = rand(0,10000000000000);

            while(
count($this->currentJobs) >= $this->maxProcesses){
               echo
"Maximum children allowed, waiting...\n";
              
sleep(1);
            }

           
$launched = $this->launchJob($jobID);
        }
      
       
//Wait for child processes to finish before exiting here
       
while(count($this->currentJobs)){
            echo
"Waiting for current jobs to finish... \n";
           
sleep(1);
        }
    }
  
   
/**
    * Launch a job from the job queue
    */
   
protected function launchJob($jobID){
       
$pid = pcntl_fork();
        if(
$pid == -1){
           
//Problem launching the job
           
error_log('Could not launch new job, exiting');
            return
false;
        }
        else if (
$pid){
           
// Parent process
            // Sometimes you can receive a signal to the childSignalHandler function before this code executes if
            // the child script executes quickly enough!
            //
           
$this->currentJobs[$pid] = $jobID;
          
           
// In the event that a signal for this pid was caught before we get here, it will be in our signalQueue array
            // So let's go ahead and process it now as if we'd just received the signal
           
if(isset($this->signalQueue[$pid])){
                echo
"found $pid in the signal queue, processing it now \n";
               
$this->childSignalHandler(SIGCHLD, $pid, $this->signalQueue[$pid]);
                unset(
$this->signalQueue[$pid]);
            }
        }
        else{
           
//Forked child, do your deeds....
           
$exitStatus = 0; //Error code if you need to or whatever
           
echo "Doing something fun in pid ".getmypid()."\n";
            exit(
$exitStatus);
        }
        return
true;
    }
  
    public function
childSignalHandler($signo, $pid=null, $status=null){
      
       
//If no pid is provided, that means we're getting the signal from the system.  Let's figure out
        //which child process ended
       
if(!$pid){
           
$pid = pcntl_waitpid(-1, $status, WNOHANG);
        }
      
       
//Make sure we get all of the exited children
       
while($pid > 0){
            if(
$pid && isset($this->currentJobs[$pid])){
               
$exitCode = pcntl_wexitstatus($status);
                if(
$exitCode != 0){
                    echo
"$pid exited with status ".$exitCode."\n";
                }
                unset(
$this->currentJobs[$pid]);
            }
            else if(
$pid){
               
//Oh no, our job has finished before this parent process could even note that it had been launched!
                //Let's make note of it and handle it when the parent process is ready for it
               
echo "..... Adding $pid to the signal queue ..... \n";
               
$this->signalQueue[$pid] = $status;
            }
           
$pid = pcntl_waitpid(-1, $status, WNOHANG);
        }
        return
true;
    }
}
up
4
arnold at helderhosting dot nl
9 years ago
It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module. You can only use pcntl_fork in CGI mode or from command-line.

Using this function will result in: 'Fatal error: Call to undefined function: pcntl_fork()'
up
2
manishpatel2280 at gmail dot com
1 year ago
Its been easy to fork process with pcntl_fork.. but how can we control or process further once all child processes gets completed.. here is the way we can do that...

<?php
for ($i = 1; $i <= 5; ++$i) {
       
$pid = pcntl_fork();

        if (!
$pid) {
           
sleep(1);
            print
"In child $i\n";
            exit(
$i);
        }
    }

    while (
pcntl_waitpid(0, $status) != -1) {
       
$status = pcntl_wexitstatus($status);
        echo
"Child $status completed\n";
    }
?>
up
1
KrazyBox
4 years ago
There are quite a few questions regarding how file descriptors get handled when processes are forked.

Remember that fork() makes a copy of the program, which means all descriptors are copied. Unfortunately, this is a rather bad situation for a PHP program because most descriptors are handled by PHP or a PHP Extension internally.

The simple, and probably "proper" way to solve this issue is to fork before hand, there really should be no need to fork at many different points among a program, you would simply fork, and then delegate the work. Use a master/worker hierarchy.

For example, if you need to have many processes that use a MySQL Connection, just fork before the connection is made, that way each child has it´s own connection to mysql that it, and it alone, manages.

With careful and correct usage, fork() can be an extremely powerful tool.

--Please remember to take proper care of your children.
up
1
John Nicholls
6 months ago
The notes on http://php.net/manual/en/function.posix-setsid.php describe how you can avoid accruing countless zombie processes by a simple call to posix_setsid() when a child starts.
up
1
kexianbin at diyism dot com
2 years ago
Fork in foreach:

<?php
foreach ($tasks as $v)
        {if ((
$pid=pcntl_fork())===-1)
            {
//...
            
continue;
            }
         else if (
$pid)
              {
pcntl_wait($status, WNOHANG); //protect against zombie children, one wait vs one child
             
}
         else if (
$pid===0)
              {
ob_start();//prevent output to main process
              
register_shutdown_function(create_function('$pars', 'ob_end_clean();posix_kill(getmypid(), SIGKILL);'), array());//to kill self before exit();, or else the resource shared with parent will be closed
               //...
              
exit();//avoid foreach loop in child process
             
}
        }
?>
up
1
nmmm at nmmm dot nu
2 years ago
It was driving me crazy that the script was killed couple of hours after I logged out, even I started it as:

php server.php >& logfile.txt

looks like PHP somehow interact with standard input, even I do not used it.

Solution was to start it with nohup:

nohup php server.php >& logfile.txt

or to do demonize / run as demon (e.g. fork() and close file descriptors)
up
1
amatsak at chestnutsoftware dot com
7 years ago
The reason for the MySQL "Lost Connection during query" issue when forking is the fact that the child process inherits the parent's database connection. When the child exits, the connection is closed. If the parent is performing a query at this very moment, it is doing it on an already closed connection, hence the error.

An easy way to avoid this is to create a new database connection in parent immediately after forking. Don't forget to force a new connection by passing true in the 4th argument of mysql_connect():

<?php
// Create the MySQL connection
$db = mysql_connect($server, $username, $password);

$pid = pcntl_fork();
            
if (
$pid == -1 ) {       
   
// Fork failed           
   
exit(1);
} else if (
$pid ) {
   
// We are the parent
    // Can no longer use $db because it will be closed by the child
    // Instead, make a new MySQL connection for ourselves to work with
   
$db = mysql_connect($server, $username, $password, true);
} else {
   
// We are the child
    // Do something with the inherited connection here
    // It will get closed upon exit
   
exit(0);
?>

This way, the child will inherit the old connection, will work on it and will close upon exit. The parent won't care, because it will open a new connection for itself immediately after forking.

Hope this helps.
up
0
somebody
4 years ago
you should be _very_ careful with using fork in scripts beyond academic examples,
or rather just avoid it alltogether, unless you are very aware of it's limitations.

the problem is that it just forks the whole php process, including not only
the state of the script, but also the internal state of any extensions loaded.
this means that all memory is copied, but all file descriptors are shared among
the parent and child processes.
and that can cause major havoc if some extension internally maintains
file descriptors.
the primary example is ofcourse mysql, but this could be any extensions that
maintains open files or network sockets.
also, just reopening your connection in the parent or child isn't a safe
method, because when the old connection resource is destroyed, the extension
might not just close it, but for example send a request to the server to log
off, making the connection unusable.
this happens with mysql for example, when php exits - in the following script the query will always fail with "MySQL server has gone away":

<?php
mysql_connect
(/* enter a working server here maybe? */);
if(
pcntl_fork()) die(); // fork a child and have the parent terminate
//if(pcntl_fork()) posix_kill(getmypid(),9); // works, but very ugly
$r=mysql_query("select 1;");
if(!
$r)die(mysql_error()."\n");
?>
(it was suggested that processes kill themselves with SIGKILL to avoid any cleanup on shutdown)

(the only save way would be to close all connections and reopen them after the fork, and even that might not be possible if an extension keeps one open internally)

for a nice demonstration of the havoc fork can create, try the below script.
it opens a mysql connection, then forks, and runs queries from both parent and child,
verifying that it receives the correct result.
run it (on the cli preferably) a few times, and you will find various possible
results:
- very often is just hangs and doesn't output anything anymore
- also very often, the server closes the connection, probably because it
  receives interleaved requests it can't process.
- sometimes one process gets the result of the OTHER processes'
  query! (because both send their queries down the same socket,
  and it's pure luck who gets the reply)

<?php
mysql_connect
(/* enter a working server here maybe? */);
$f=pcntl_fork();
while(
true){
   
sleep(rand(0,10)/100);
   
$r=mysql_query("select $f;");
    if(!
$r)die($f.": ".mysql_error()."\n");
    list(
$x)=mysql_fetch_array($r);
    echo (
$f)?".":"-";
    if(
$x!=$f) echo ($f.": fail: $x!=$f\n ");
}
?>
up
0
Anonymous
4 years ago
With regards to the database connection, one could deal with this using kill 9 or a sleep, the real problem is if two threads make a database query at the same time, PHP starts having random database errors that are not necessarily clear as to what the problem is.

You should create a separate link per thread.
up
0
php at mx dot magic-lamp dot org
4 years ago
A workaround for the MySQL "Lost Connection during query", or any other object related problems caused by children exiting is to force the child to kill -9 itself, thus avoiding any cleanup.  Sure - it's not too elegant, but it does work.

<?php
$pid
= pcntl_fork();
if (
$pid == 0 ) {
   
// This is the child process.  Do something here.
    // Instead of calling exit(), we use posix_kill()
   
posix_kill(getmypid(),9);
}
?>

Watch out that you don't spawn too many processes though as this creates its own problems.
up
0
drrota at us dot ibm dot com
5 years ago
I was able to get around the problem of not being able to run fork and exec from Apache php.

I got around this by calling the system 'at' command on Linux.  "at run something now".  and you have to set atrun -s in a crontab file (to run every minute) to insure that things get kicked off quickly even if there is a heavy load on the machine.

If you're the only one running batch jobs on a linux box, this works.
up
0
kentmussell at mindspring dot com
6 years ago
Here is an interesting script I wrote.  It demonstrates how pcntl_fork() might be used as a useful tool.

<?php
/* This script serves the purpose of testing an algorithm designed to:
a.) Compare password hashes, or try passwords efficiently where the time to try a single password is 10 seconds. 
b.) Spawn threads to work simultaneously on comparing hashes.
c.) Restrict the number of threads open at a time. 
*/
//checks for divisibility
function divby($num,$den) {
   
$result = $num/$den;
   
$result2 = floor($result);
    if (
$result == $result2) {
        return
true;
        }
    else {
        return
false;
        }
    }
//checks whether a period of time fits into 2 second intervals occuring every 10 seconds.  Interval may increase or decrease in size to use more or less memory. 
function goodTime($elapsed) {
   
$num = floor($elapsed);
   
$num = $num/12;
   
$min = floor($num);
   
$min = 12*$min;
   
$max = $min+2;
    if (
$elapsed >= $min && $elapsed <= $max) {
        return
"yes";
        }
    else {
        return
"no";
        }
    }

$x = 30; //number of child threads
$pid = 1; //needed to create first thread
$xpass = md5('29');//hash to crack
$time = time();
$i = 1;
//parent spawns $x children.
while ($i <= $x) {
    if (
file_exists('childcall.txt')) {
       
unlink('childcall.txt');
        exit;
        }
   
$elapsed = time()-$time;
   
//children are only spawned during intervals occuring every 10 seconds leaving enough time for the previous batch of children to finish their task.
   
if (goodTime($elapsed)=="yes") {   
       
//Are we the parent?
       
if ($pid != 0) {
           
//Give birth to a child. 
           
$pid = pcntl_fork();
           
//create a record of how many children have been birthed.
           
$arr[$i] = $i;
           
$time2 = $elapsed;
            }
       
//escort children out of the loop.
       
if ($pid == 0) {
           
$i = $x+1;
            }
       
$i++;
        }
    }
//parent waits for children to finish playing.
if ($pid) {
   
$value = 1;
    while (!
file_exists('childcall.txt')) {
       
//wait
       
}
   
unlink('childcall.txt');
   
$time = time()+2;
    while (
time()<$time) {
       
//wait
       
}
    exit;
    }
//children take turns finding the highest array value, and changing it to 0
rsort($arr);
$value = max($arr);
$arr[$value] = 0;
$time = time()+10;
//simulate delay
while (time() < $time) {
   
//wait
   
}
//compare the high array value hash to the hash we are looking to crack.
if (md5($value) == $xpass) {
    echo
"$value \n";
    }
if (
$value == $x || md5($value) == $xpass) {
   
$file = "childcall.txt";
   
$content = true;
   
file_put_contents($file,$contents);
    }
?>
up
0
ben at gelbnet dot com
11 years ago
I was writing a shell script to get input from a user, however, I needed my script to time out after a certain number of seconds if the user didn't enter enough data. The code below descibes the method I used. It's a little hairy but it does work.

-Ben

#!/home/ben/php/bin/php -q
<?php
//GLOBALS
$RETURN_CHAR = "\n";
$TIMEOUT = 5; //number of seconds to timeout on input
$PID = getmypid();
$CHILD_PID = 0;

//Make sure program execution doesn't time out
set_time_limit(0);

function
set_timeout() {
global
$PID;
global
$CHILD_PID;
global
$TIMEOUT;

$CHILD_PID = pcntl_fork();
if(
$CHILD_PID == 0) {
sleep($TIMEOUT);
posix_kill($PID, SIGTERM);
exit;
}
}

function
clear_timeout() {
global
$CHILD_PID;
posix_kill($CHILD_PID, SIGTERM);
}

// read_data()
// gets a line of data from STDIN and returns it
function read_data() {

$in = fopen("php://stdin", "r");
set_timeout();
$in_string = fgets($in, 255);
clear_timeout();
fclose($in);
return
$in_string;
}

// write_data($outstring)
// writes data to STDOUT
function write_data($outstring) {
$out = fopen("php://stdout", "w");
fwrite($out, $outstring);
fclose($out);
}

while(
1) {
write_data("say something->");
$input = read_data();
write_data($RETURN_CHAR.$input);
}

?>
up
-2
xuecan at google dot com
8 years ago
I think this simple code can help understand how fork works:

<?php
echo "posix_getpid()=".posix_getpid().", posix_getppid()=".posix_getppid()."\n";

$pid = pcntl_fork();
if (
$pid == -1) die("could not fork");
if (
$pid) {
    echo
"pid=".$pid.", posix_getpid()=".posix_getpid().", posix_getppid()=".posix_getppid()."\n";
} else {
    echo
"pid=".$pid.", posix_getpid()=".posix_getpid().", posix_getppid()=".posix_getppid()."\n";
}
?>
To Top