Question

I was working around with pcntl_fork() of php, i noticed that executing from the CLI i got the correct result but when i executed it from apache it gives me exception 'Undefined function pcntl_fork()'

And yes i read that executing via apache is not safe enough !!

My questions :

Is there any work around that lets me execute the php script having pcntl_fork() implementation via apache ?

Why in the first place it is not safe to execute forking via apache but safe enough from CLI ?

Was it helpful?

Solution

If, for example, you want the script to continue running even after the user cancelled the page request (pressing "stop" on their browser or "back" or "refresh" etc), then you might want to consider ignore_user_abort http://www.php.net/manual/en/function.ignore-user-abort.php.

As for why it is not allowed to fork from apache, it's because how the way PHP is interfaced with Apache. I don't have deep knowledge in this area to explain in detail, but it's probably like this:

If you use mod_php, Apache spawns a new PHP process for every request. This means apache needs to keep track of each process and kill the process once the request ends.

For mod_fcgi and the others (I think) however, one PHP process is spawned for several number of requests, this means that the process will keep running and accept new requests. However to control memory leaks, those child PHP processes are killed periodically.

And now, if you try to spawn a new process within these PHP process child, well frankly I don't know what will happen. It'll be most likely that the process spawned will not get killed and continue hogging the resources.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top