PHP fork child process blocks parent process

Posted on 2007-10-02
Last Modified: 2013-12-25
I am trying to use PHP fork abilities to run some database queries in the background (triggered by visiting a  web page) but when the fork happens the parent process does not exit until the child is finished causing the browser to appear as if the page has not finished loading.

I am running the script as cgi so it runs using the CLI version of PHP. There are no errors reported. My guess is that the child maintains connection to STDOUT causing the problem.

Here is code that reproduces my problem. Notice the web page will act like it is loading for about 11 seconds.

$parentPID = posix_getpid();
echo "Contact-Type: text/html\n\n";

$pid = pcntl_fork();
if ($pid == -1){
      die("could not fork");
else if ($pid){
      exit(); // we are the parent
      // we are the child
      $childPID = posix_getpid();

// detatch from the controlling terminal
if (posix_setsid() == -1){
      die("could not detach from terminal");

echo "MyPID: ".posix_getpid(); // this is sent to the browser, but we should be the child...

if(!posix_kill($parentPID, SIGTERM)){
      // I tried this but it does not help
      die("could not kill parent");

// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");

sleep(10); // this simulates the custom code I will use

function sig_handler($signo){
      switch ($signo) {
            case SIGTERM:
                  // handle shutdown tasks
            case SIGHUP:
                  // handle restart tasks
                  // handle all other signals

Question by:terabit_ca
    LVL 48

    Accepted Solution

    Thats the way forks work in php. The parent process will end after the child has finished. Better use exec to start a new process. See :

    Note: If you start a program using this function and want to leave it running in the background, you have to make sure that the output of that program is redirected to a file or some other output stream or else PHP will hang until the execution of the program ends.

    exec('foo & 1>/dev/null');
    LVL 6

    Expert Comment

    Generally not all commands could be executed using exec.
    The server imposes restrictions to what type of commands are fired from web.

    My idea would be to show a message showing something like "Processing Request" when we fork the child and after child finishes show "Processed the request".

    Forking only helps in overcoming the timeout problem with heavy queries.

    Another way would be that you set the <form target="_blank"> so that when the process starts it starts in a new window, in this way user will still be able to browse other parts of the website.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    This video teaches users how to migrate an existing Wordpress website to a new domain.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now