How Does Pcntl_Fork Work in PHP

how to use pcntl_fork() with Apache?

This is the solution:

posix_kill(getmypid(), SIGKILL);

instead of exit().

PHP: What does pcntl_fork() really do?

pcntl_fork probably works as you think it would : it forks the current process, the same way the C function fork does :

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.


But, quoting the Introduction of the Process Control section of the manual :

Process Control support in PHP
implements the Unix style of process
creation, program execution, signal
handling and process termination.

Process Control should not be
enabled within a web server
environment and unexpected results may
happen if any Process Control
functions are used within a web server
environment.

So, you should not actually use that function from a PHP script executed via Apache : it should only be used when your PHP script is executed from the command-line.


And, before starting to use that function, don't forget that :

Note: This extension is not available
on Windows platforms.

What happens behind the PHP pcntl_fork()?

This is basically what happens:

                    pcntl_fork()
||
+----------------++------------------+
parent child

"start" ...
"parent" ...
"end" ...
"start"
"child"
"end"

Any code after pcntl_fork() gets executed by both parent and child process; you can think of the child as a clone of the parent, except that the outcome of pcntl_fork() is 0; it's like the code enters a different reality, not unlike Back to the future ;-)

This is just one of the possible outcomes, though; depending on cpu usage, the child's "start" may occur together with the parent's "parent" output.

PHP pcntl_fork in a foreach loop

pcntl_fork() creates a copy of the script which is at the same point of execution as the parent, as you've noticed.

You can check to see if this script is the parent or the child by checking the return of pcntl_fork():

foreach($namesas $name)
{
$pid = pcntl_fork();
if(!$pid){
//We are the child
echo ++$count."\n";
exec("python script.py -argument:".$name);
break;//Jump out of loop in this child. Parent will continue.
}
}

process from pcntl_fork not terminating

You should have to use this function:

http://php.net/manual/en/function.pcntl-wait.php

But generally under Apache forking is probably not a good idea.

Unexpected cores synchronization in pcntl_fork()

I've had chance to repeat experiment on machine with 4 physical cores. lscpu output about machine CPU specification:

CPU(s): 4

On-line CPU(s) list: 0-3

Thread(s) per core: 1

Core(s) per socket: 4

Socket(s): 1

And I've got such CPU load graph when running experiment:
Sample Image

So it can be seen that in this case there is no cross-core synchronization and each core is loaded with about twice as many work as previous one.
Confirmed that above mentioned effect is related that test machine had 4 virtual cores (2 physical x 2 hyper-threads) and not just 4 physical ones.



Related Topics



Leave a reply



Submit