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 9182249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:30:57+00:00 2026-06-17T18:30:57+00:00

I have passed hours and days to find why php share the memory between

  • 0

I have passed hours and days to find why php share the memory between my forked children and I figured that if the parent set a var in a function before forking then the function will always return the same result in the children :~

new mytest;
class mytest{
    var $t = 'Parent';
    public function __construct(){              
        //$this->runChildProcess($i);       

        $pidsCount = 0;
        for($i = 0; $i < 5; $i++) { 
            $pids[$pidsCount] = pcntl_fork();
            if($pids[$pidsCount]) {
                $this->t = 'Parent';
                // i am the parent
                $pidsCount++;
            }
            else {
                $this->t = 'Enfant';
                // i am the child
                $this->runChildProcess($i);
                exit();
            }
        }
        for($i = 0; $i < $pidsCount; $i++) {
            pcntl_waitpid($pids[$i], $status, WUNTRACED);
        }
    }

    function runChildProcess($i) {
        $a = rand(0,100)."\n";
        echo $this->t.' : '.$a;
    }   
}

If you run this sample it’ll be just fine, the children will output different numbers.
But if you un-comment the first $this->runChildProcess($i); then you’ll see that all the children will return the same result (the first one calculated by a children)

I don’t know how to deal with that 🙁

  • 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-17T18:30:58+00:00Added an answer on June 17, 2026 at 6:30 pm

    I don’t think this is anything to do with memory sharing. You’re just seeing the behaviour of rand.

    With that line commented out, each child calls rand for the first time, so each gets seeded independently.

    With that line uncommented, then rand is seeded before any of the children are forked. So they all see the same seed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How To get the number of days that have already passed from the current
How can I find out how many hours have passed been since a record
I have passed the last couple of hours trying to downgrade Java to 1.6.0_13
Good day - several days have passed while i was trying to transport my
I have been passed a date string that looks like this: Thu%20Mar%2011%202010%2015%3A09%3A11%20GMT%2B0000%20(BST) I want
I'm attempting to build a method call from strings that have been passed into
I have some code that is taking a long time to run(several hours) and
Possible Duplicate: count date difference in hours using php and mysql I have a
I have passed to template dictionary parameters like parameters={ 'value':'some_value', 'style':{} } and inside
I have passed Python list to template like l=['text','there is no salary','I need alcohol','and

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.