Re: Re: Pass variable to pcntl_fork child

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jul 2, 2014 at 11:31 AM, Uggla Henrik <Henrik.Uggla@xxxxxxxxxxxxxxx>
wrote:

>
> ________________________________________
> Från: Aziz Saleh [azizsaleh@xxxxxxxxx]
> Skickat: den 2 juli 2014 16:57
> Till: webmaster@xxxxxxxxxxxx
> Kopia: php-general@xxxxxxxxxxxxx
> Ämne: Re:  Re: Pass variable to pcntl_fork child
>
> On Wed, Jul 2, 2014 at 10:41 AM, Jim Giner <jim.giner@xxxxxxxxxxxxxxxxxx>
> wrote:
>
> > On 7/2/2014 5:40 AM, Uggla Henrik wrote:
> >
> >> Hi!
> >>
> >> I've tried the following code but the $call variable is not available in
> >> the pcntl_fork child. Searching the web, I've found posts that claim
> that
> >> all variables defined before pcntl_fork are copied to the child. I've
> also
> >> found posts that claim that no variables are passed/copied to the child.
> >> So, which of them is true and how should I correct my code to make it
> work?
> >>
> >> regards
> >> Henrik
> >>
> >>
> >>
> >>      $call = str_replace('@@', '&', $_GET['call']);
> >>
> >>      if ($pid = pcntl_fork())
> >>      {
> >>          $previousCalls=file("calls.txt", FILE_IGNORE_NEW_LINES |
> >> FILE_SKIP_EMPTY_LINES);
> >>          if (!in_array($call, $previousCalls))
> >>          {
> >>              file_put_contents("calls.txt", $call."\n", FILE_APPEND);
> >>          }
> >>      }
> >>      else
> >>      {
> >>          function displayUrl($url)
> >>          {
> >>              $ch = curl_init($url);
> >>              curl_setopt ( $ch, CURLOPT_HEADER, 1);
> >>              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
> >>              $curlResult = curl_exec($ch);
> >>              curl_close($ch);
> >>              list($headers,$content) =
> explode("\r\n\r\n",$curlResult,2);
> >>              foreach (explode("\r\n",$headers) as $hdr)
> >>              {
> >>                  if ($hdr != 'Transfer-Encoding: chunked')
> >>                  {
> >>                      header($hdr);
> >>                  }
> >>              }
> >>              echo $content;
> >>          }
> >>          displayUrl($call);
> >>      }
> >>
> >>  1 What's a child?
> > 2 You must use == in your if condition.
> > 3 ??? Why do you have a function defined in your else clause???
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> The only issue I see in your code is that the parent is not waiting for the
> child to close, which can cause zombies. Add:
>
> pcntl_wait($status);
>
> As shown here: http://www.php.net/manual/en/function.pcntl-fork.php
>
> Also you are not checking if the fork failed (-1), shown on the above
> example.
>
> Your code with the wait works fine for me (outputs data and records data in
> the file).
>
>
> Does the child echo to the browser that started the php or does the output
> go somewhere else?
>
> /H


No I tested that on command prompt not the browser. Someone else will have
to test it on a browser to see how it works.

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux