SV: Pass variable to pcntl_fork child

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

 



I switched places of the parent and child code. The parent always did what it was supposed to, the child never did.

/H
________________________________________
Från: Sebastian Krebs [krebs.seb@xxxxxxxxx]
Skickat: den 2 juli 2014 12:29
Till: Uggla Henrik
Kopia: php-general@xxxxxxxxxxxxx
Ämne: Re:  Pass variable to pcntl_fork child

2014-07-02 11:40 GMT+02:00 Uggla Henrik <Henrik.Uggla@xxxxxxxxxxxxxxx>:

> 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.


Not directly "copied", at least not explicitely. 'pcntl_fork()' is derived
from the system call 'fork()', which roughly behave like this

- pause execution
- Create a second (child-)process B
- _clone_ the _entire_ memory used by this process A to the memory of B
- resume execution

So after forking the child process it is an exact clone of the parent,
including all variables and stuff. The only difference is the PID and
therefore (somehow) the return value of pctnl_fork().

This said: Yeah, $call should be there. Have a look at http://3v4l.org/cV73d
That only works for HHVM there, but I promise you, that I had reproduced it
locally with PHP5.5 ;)


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?
>

Maybe a different question: How did you fount out, that $call doesn't
contain, what you expected? What was the error?


>
> 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);
>     }
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
github.com/KingCrunch

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php






[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