PHP: What does pcntl_fork() really do?
Asked Answered
F

4

13

PHP's pcntl_fork function is supposed to fork a process just as the standard fork function in C.
But I was wondering if this function really forks the process or if it emulates that behavior in a different way.
If it really forks the process then it's clear which process that is: one of Apache's child processes.
That's OK as long as Apache is using the prefork MPM (i.e. one process per request).
But what does happen if Apache is using the worker MPM??
When the worker MPM is being used, every Apache child process contains many threads, each one handling a different HTTP request. So if you would fork the process under that situation I can't even think what would happend to all those threads and requests being served.
So if pcntl_fork() really forks the process then I think it's not a good idea to use this function if you set Apache to use the worker MPM.

What do the experts say? Am I reasoning well, or I'm just speaking nonsense?

Folderol answered 10/1, 2010 at 20:59 Comment(2)
Related material can be found in the Chapter about Process Control in tuxradar.com/practicalphp/16/1/0Minos
take a look at #12215285Pertain
I
22

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.

Iva answered 10/1, 2010 at 21:10 Comment(3)
"Process Control should not be enabled within a web server environment" => Must remember to always read introductions!Folderol
@PascalMARTIN, ? What does that comment mean?Gusella
@Gusella different web servers manage the process lifecycle of language interpreters (such as PHP) differently and pass data to them differently. Depending on your specific software stack, you may not have certain $_SEVER values available and other data, for example. Your process may be closed or optimized from outside of your scripts as well. Using PHP-FPM, which runs as a daemon and is not managed but communicated to, is a simple way to avoid some of these pitfalls.Mercuric
S
3

It's not a good idea to run PHP as a module on an Apache installation configured for worker MPM in the first place, because PHP is not threadsafe (I think that is stated somewhere in the PHP manual too).

It should fork the process, yes. The PHP manual even states that you should read man fork(2) for further instructions, so it's probably just a wrapper around the C fork function.

Update: Here's the relevant page in the PHP manual for worker MPM: http://php.net/install.unix.apache2.php

Note: To build a multithreaded version of Apache, the target system must support threads. In this case, PHP should also be built with experimental Zend Thread Safety (ZTS). Under this configuration, not all extensions will be available. The recommended setup is to build Apache with the default prefork MPM-Module.

I also found this page with some further instructions: http://www.stevekallestad.com/blog/apache_worker_mpm_with_php.html

Sunburst answered 10/1, 2010 at 21:4 Comment(1)
PHP is just as threadsafe as C# or C++ -- thread safety is implementation defined. (yes even in 2010) Common, especially in 2010, deployments of Apache however do not use PHP in a thread safe manner. Thread safety in NGINX + PHP-FPM stacks, then and now, is fairly straight-forwardMercuric
T
3

I'll try to be quick and concise,

Using "fork" through apache it is possible, you need to "install" then enable the functions in the php.ini, finally you need to add the extension in the apache directori (a symlink would have the job done as well) Ex:

echo "extension=pcntl.so" > /etc/php5/conf.d/pcntl.ini
ln -s /etc/php5/apache2/conf.d/pcntl.ini /etc/php5/mods-available/pcntl.ini 

On the other hand, I've been using forking for a lot of projects and it is really great at optimizing most of them, however, there is a bug when abusing of it with apache, im basically forking the forked child and doing anykind of hardcore stuff and it works... pretty good, but under load it works for some time before start to creating zombie process, i can manage the zombie process using "pcntl_signal(SIGCHLD, SIG_IGN);" which basically will remove the process as soon as the child finish their task, this help a little bit, then is when apache goes crazy, and start threading itself and finally crashing your server, i cannot explain this behavior (yet, but i will) this weir/evil tree created by apache can only be seen from "ps" nor from server-status or apache logs, and i said evil tree because it basically creates hundred of process with children of children...

in nutshell:

Fork with apache will work? YES... absolutely

just dont abuse of it

hope this will help some one

Tripitaka answered 11/1, 2016 at 15:38 Comment(0)
M
0

I have just tried to use pcntl_fork via apache, the strange situation is that after fork a child process, the parent give the standard output(browser) to its child. So, you can image, the browser cann't receive the output from the parent process.

Morril answered 6/7, 2012 at 8:53 Comment(1)
Read the accepted answer and, particularly, my comment to that answer.Folderol

© 2022 - 2024 — McMap. All rights reserved.