Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7805961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:22:47+00:00 2026-06-02T02:22:47+00:00

My issue is this. I am forking a process so that I can speed

  • 0

My issue is this. I am forking a process so that I can speed up access time to files on disk. I store any data from these files in a tmp file on local desk. ideally, after all processes have finished, I need to access that tmp file and get that data into an array. I then unlink the tmp file as it is no longer needed. My problem is that it would seem that pcntl_wait() does not acutally wait until all child processes are done before moving on to the final set of operations. So I end up unlinking that file before some random process can finish up.

I can’t seem to find a solid way to wait for all processes to exit cleanly and then access my data.

    $numChild       = 0;    
    $maxChild       = 20;   // max number of forked processes.

    // get a list of "availableCabs"

    foreach ($availableCabs as $cab) {

            // fork the process
            $pids[$numChild] = pcntl_fork();

            if (!$pids[$numChild]) {

                    // do some work     
                    exit(0);

            } else {

                    $numChild++;
                    if ($numChild == $maxChild) {

                            pcntl_wait($status);
                            $numChild--;

                    } 

            } // end fork   

    }

    // Below is where things fall apart. I need to be able to print the complete serialized data. but several child processes don't actually exit before i unlink the file.

    $dataFile = fopen($pid, 'r');

    while(($values = fgetcsv($dataFile,',')) !== FALSE) {
            $fvalues[] = $values;
    }

    print serialize($fvalues);

    fclose($dataFile);
    unlink($file);      

please note that i’m leaving a lot of code out regarding what i’m actually doing, if we need that posted thats not issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:22:49+00:00Added an answer on June 2, 2026 at 2:22 am

    Try restructuring you code so that you have two loops – one that spawns processes and one that waits for them to finish. You should also use pcntl_waitpid() to check for specific process IDs, rather than the simple child counting approach you are currently using.

    Something like this:

    <?php
    
      $maxChildren = 20;   // Max number of forked processes
      $pids = array();     // Child process tracking array
    
      // Get a list of "availableCabs"
    
      foreach ($availableCabs as $cab) {
    
        // Limit the number of child processes
        // If $maxChildren or more processes exist, wait until one exits
        if (count($pids) >= $maxChildren) {
          $pid = pcntl_waitpid(-1, $status);
          unset($pids[$pid]); // Remove PID that exited from the list
        }
    
        // Fork the process
        $pid = pcntl_fork();
    
        if ($pid) { // Parent
    
          if ($pid < 0) {
            // Unable to fork process, handle error here
            continue;
          } else {
            // Add child PID to tracker array
            // Use PID as key for easy use of unset()
            $pids[$pid] = $pid;
          }
    
        } else { // Child
    
          // If you aren't doing this already, consider using include() here - it
          // will keep the code in the parent script more readable and separate
          // the logic for the parent and children
    
          exit(0);
    
        }
    
      }
    
      // Now wait for the child processes to exit. This approach may seem overly
      // simple, but because of the way it works it will have the effect of
      // waiting until the last process exits and pretty much no longer
      foreach ($pids as $pid) {
        pcntl_waitpid($pid, $status);
        unset($pids[$pid]);
      }
    
      // Now the parent process can do it's cleanup of the results
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT I solved the issue by forking the process instead of using threads. From
How can I ignore the attribute error I get when I issue this command?
I have this issue with ComboBox that uses IEnumerable<Brush> as ItemsSource; the problem lies
Our nightly build process was broken for a long time, such that it generated
The URL http://www.fourmilab.ch/cgi-bin/Earth shows a live map of the Earth. If I issue this
This issue has me baffled, it's affecting a single user (to my knowledge) and
Note: This issue appears to be limited to SQL Server 2005 SP2 I have
EDIT: This issue is already submitted on Github. Changing the Act part to this
This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
Update: This issue was not properly explored. The real issue lies within render :json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.