Discussion:
Pass variable to pcntl_fork child
Uggla Henrik
2014-07-02 09:40:25 UTC
Permalink
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);
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
helloworldjerry
2014-07-02 10:17:55 UTC
Permalink
ipc can be used by shared memory, senaphore, queue, socket, unix domain socket etc.
variables should be shared copied to parent and child process both after fork.
u can store some variable at third party storage, like redid, and after that signal child process where u fetch it back.
And some other ipc means above u can try , too. 
-- 
helloworldjerry

匀启 2014幎7月2日 at 18:01:20, Uggla Henrik (***@kristianstad.se) 写:

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);
}


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sebastian Krebs
2014-07-02 10:29:32 UTC
Permalink
Post by Uggla Henrik
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
Post by Uggla Henrik
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?
Post by Uggla Henrik
regards
Henrik
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
Uggla Henrik
2014-07-02 14:37:06 UTC
Permalink
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 [***@gmail.com]
Skickat: den 2 juli 2014 12:29
Till: Uggla Henrik
Kopia: php-***@lists.php.net
Ämne: Re: [PHP] Pass variable to pcntl_fork child
Post by Uggla Henrik
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
Post by Uggla Henrik
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?
Post by Uggla Henrik
regards
Henrik
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
Jim Giner
2014-07-02 14:41:24 UTC
Permalink
Post by Uggla Henrik
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
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
Aziz Saleh
2014-07-02 15:00:56 UTC
Permalink
Post by Jim Giner
Post by Uggla Henrik
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
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
Jim,

Please fix your reply to email (it throws a 550). As stated on the
guidelines: http://php.net/mailing-lists.php reply-all is the default post
method to the mailing list, every time I do that I get a failed email
address: ***@jimginer.net.

Thanks,
Aziz
Christoph Becker
2014-07-02 15:11:22 UTC
Permalink
Post by Jim Giner
Post by Uggla Henrik
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?
In this case a child process, see
<http://www.php.net/manual/en/function.pcntl-fork.php>.
Post by Jim Giner
2 You must use == in your if condition.
In this case that would be wrong, see
<http://www.php.net/manual/en/function.pcntl-fork.php>.
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2014-07-02 15:19:32 UTC
Permalink
Post by Christoph Becker
Post by Jim Giner
Post by Uggla Henrik
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?
In this case a child process, see
<http://www.php.net/manual/en/function.pcntl-fork.php>.
Post by Jim Giner
2 You must use == in your if condition.
In this case that would be wrong, see
<http://www.php.net/manual/en/function.pcntl-fork.php>.
So I see....
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Uggla Henrik
2014-07-02 15:19:25 UTC
Permalink
________________________________________
Från: Jim Giner [***@albanyhandball.com]
Skickat: den 2 juli 2014 16:41
Till: php-***@lists.php.net
Ämne: [PHP] Re: Pass variable to pcntl_fork child
Post by Uggla Henrik
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
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

3. I did that to make it easier to follow the code.

/H
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-07-02 14:57:17 UTC
Permalink
Post by Jim Giner
Post by Uggla Henrik
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
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).
Uggla Henrik
2014-07-02 15:31:19 UTC
Permalink
________________________________________
Från: Aziz Saleh [***@gmail.com]
Skickat: den 2 juli 2014 16:57
Till: ***@jimginer.net
Kopia: php-***@lists.php.net
Ämne: Re: [PHP] Re: Pass variable to pcntl_fork child
Post by Jim Giner
Post by Uggla Henrik
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
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-07-02 15:37:42 UTC
Permalink
Post by Uggla Henrik
________________________________________
Skickat: den 2 juli 2014 16:57
Ämne: Re: [PHP] Re: Pass variable to pcntl_fork child
Post by Jim Giner
Post by Uggla Henrik
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
Post by Jim Giner
Post by Uggla Henrik
all variables defined before pcntl_fork are copied to the child. I've
also
Post by Jim Giner
Post by Uggla Henrik
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?
Post by Jim Giner
Post by Uggla Henrik
regards
Henrik
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);
Post by Jim Giner
Post by Uggla Henrik
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
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.
Jim Lucas
2014-07-02 21:34:13 UTC
Permalink
Post by Jim Giner
Post by Uggla Henrik
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
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?
If you don't understand this, please read this page:

http://www.php.net/manual/en/function.pcntl-fork.php
Post by Jim Giner
2 You must use == in your if condition.
Look at the first example on the above page.
Post by Jim Giner
3 ??? Why do you have a function defined in your else clause???
The only reason I can think of would be to isolate the inside code from the
work. But, this could also lead to the missing variable issue the OP is
asking about.
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Szopen Xiao
2014-07-03 02:12:47 UTC
Permalink
I think your fork is failure
when pcntl_fork() error will return -1, and -1 == true equal true
Post by Uggla Henrik
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
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
louis
2014-07-03 03:45:53 UTC
Permalink
Child process should has the same environment as the parent process. I
don't see any thing wrong with your code.
I assume that you are using php to handle http request for the using of
$_GET , but I think it is wrong to use multi process in PHP in the HTTP
SERVER environment
Post by Uggla Henrik
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
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
louis
2014-07-03 07:26:31 UTC
Permalink
It's easy, you write a rest API to do the write staff, and in your main
code , you just have to request the rest API and do not wait it to return
But how should I do in the HTTP SERVER environment? I don't want php to wait until the file write is finished before the url is displayed. I've tried to use flush, ob_flush but that didn't work, possibly because I have session_start in the beginning of my code. Should I use exec to start a second php?
/H
________________________________________
Skickat: den 3 juli 2014 05:45
Ämne: Re: [PHP] Pass variable to pcntl_fork child
Child process should has the same environment as the parent process. I
don't see any thing wrong with your code.
I assume that you are using php to handle http request for the using of
$_GET , but I think it is wrong to use multi process in PHP in the HTTP
SERVER environment
Post by Uggla Henrik
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
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sebastian Krebs
2014-07-03 07:35:41 UTC
Permalink
Post by louis
It's easy, you write a rest API to do the write staff, and in your main
code , you just have to request the rest API and do not wait it to return
I don't know, if I get you right

- If you _provide_ an API. In that case the request should actually handle
the request only and therefore there is no need to free the process from
the burden to answer the request.
- If you _consume an API, than I'd recommend using non-blocking streams
instead. There are much easier to handle.
Post by louis
But how should I do in the HTTP SERVER environment? I don't want php to
wait until the file write is finished before the url is displayed. I've
tried to use flush, ob_flush but that didn't work, possibly because I have
session_start in the beginning of my code. Should I use exec to start a
second php?
/H
________________________________________
Skickat: den 3 juli 2014 05:45
Ämne: Re: [PHP] Pass variable to pcntl_fork child
Child process should has the same environment as the parent process. I
don't see any thing wrong with your code.
I assume that you are using php to handle http request for the using of
$_GET , but I think it is wrong to use multi process in PHP in the HTTP
SERVER environment
Post by Uggla Henrik
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
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
github.com/KingCrunch
louis
2014-07-03 07:55:55 UTC
Permalink
well right! Just forgot to use stream_set_blocking(..., ...) to set a
stream non-blocking mode.
Post by louis
It's easy, you write a rest API to do the write staff, and in your
main code , you just have to request the rest API and do not wait
it to return
I don't know, if I get you right
- If you _provide_ an API. In that case the request should actually
handle the request only and therefore there is no need to free the
process from the burden to answer the request.
- If you _consume an API, than I'd recommend using non-blocking
streams instead. There are much easier to handle.
But how should I do in the HTTP SERVER environment? I don't
want php to wait until the file write is finished before the
url is displayed. I've tried to use flush, ob_flush but that
didn't work, possibly because I have session_start in the
beginning of my code. Should I use exec to start a second php?
/H
________________________________________
Skickat: den 3 juli 2014 05:45
Ämne: Re: [PHP] Pass variable to pcntl_fork child
Child process should has the same environment as the parent process. I
don't see any thing wrong with your code.
I assume that you are using php to handle http request for the using of
$_GET , but I think it is wrong to use multi process in PHP in the HTTP
SERVER environment
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
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
github.com/KingCrunch <https://github.com/KingCrunch>
louis
2014-07-03 08:12:26 UTC
Permalink
Just google the stream_set_blocking a while and see some code examples,
it's easy to use.
This is a bit over my head. Could you explain more or give me some links?
/H
________________________________
Skickat: den 3 juli 2014 09:55
Till: Sebastian Krebs
Ämne: Re: [PHP] Pass variable to pcntl_fork child
well right! Just forgot to use stream_set_blocking(..., ...) to set a stream non-blocking mode.
It's easy, you write a rest API to do the write staff, and in your main code , you just have to request the rest API and do not wait it to return
I don't know, if I get you right
- If you _provide_ an API. In that case the request should actually handle the request only and therefore there is no need to free the process from the burden to answer the request.
- If you _consume an API, than I'd recommend using non-blocking streams instead. There are much easier to handle.
But how should I do in the HTTP SERVER environment? I don't want php to wait until the file write is finished before the url is displayed. I've tried to use flush, ob_flush but that didn't work, possibly because I have session_start in the beginning of my code. Should I use exec to start a second php?
/H
________________________________________
Skickat: den 3 juli 2014 05:45
Ämne: Re: [PHP] Pass variable to pcntl_fork child
Child process should has the same environment as the parent process. I
don't see any thing wrong with your code.
I assume that you are using php to handle http request for the using of
$_GET , but I think it is wrong to use multi process in PHP in the HTTP
SERVER environment
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
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
github.com/KingCrunch <https://github.com/KingCrunch>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sebastian Krebs
2014-07-03 08:37:23 UTC
Permalink
This is a bit over my head. Could you explain more or give me some links?
If you have a stream, like a file-, or a socket-stream, usually when you
read from it, the process blocks until the system has read EOF
(end-of-file), or the X bytes you requested. In non-blocking mode it does
not block, but return with whatever is there, even if there is nothing ("0
byte"). The technically part is, that the OS writes everything into a
read-buffer first.

Practically this means, that you open a socket to the remote host, send
your request ("write"), then do something else, in between you read what is
already in the buffer, than do something else (and so on), and at the end,
when you receive the EOF ("fread" returns null afair), you can evaluate the
response.

Or even easier: Use a client library like Guzzle :) Don't reinvent the
wheel ;)
/H
________________________________
Skickat: den 3 juli 2014 09:55
Till: Sebastian Krebs
Ämne: Re: [PHP] Pass variable to pcntl_fork child
well right! Just forgot to use stream_set_blocking(..., ...) to set a
stream non-blocking mode.
It's easy, you write a rest API to do the write staff, and in your main
code , you just have to request the rest API and do not wait it to return
I don't know, if I get you right
- If you _provide_ an API. In that case the request should actually handle
the request only and therefore there is no need to free the process from
the burden to answer the request.
- If you _consume an API, than I'd recommend using non-blocking streams
instead. There are much easier to handle.
But how should I do in the HTTP SERVER environment? I don't want php to
wait until the file write is finished before the url is displayed. I've
tried to use flush, ob_flush but that didn't work, possibly because I have
session_start in the beginning of my code. Should I use exec to start a
second php?
/H
________________________________________
Skickat: den 3 juli 2014 05:45
Ämne: Re: [PHP] Pass variable to pcntl_fork child
Child process should has the same environment as the parent process. I
don't see any thing wrong with your code.
I assume that you are using php to handle http request for the using of
$_GET , but I think it is wrong to use multi process in PHP in the HTTP
SERVER environment
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
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
github.com/KingCrunch <https://github.com/KingCrunch>
--
github.com/KingCrunch
louis
2014-07-03 07:31:41 UTC
Permalink
If you're using php in HTTP environment, it is safer to do your staff in
a http way.
But how should I do in the HTTP SERVER environment? I don't want php to wait until the file write is finished before the url is displayed. I've tried to use flush, ob_flush but that didn't work, possibly because I have session_start in the beginning of my code. Should I use exec to start a second php?
/H
________________________________________
Skickat: den 3 juli 2014 05:45
Ämne: Re: [PHP] Pass variable to pcntl_fork child
Child process should has the same environment as the parent process. I
don't see any thing wrong with your code.
I assume that you are using php to handle http request for the using of
$_GET , but I think it is wrong to use multi process in PHP in the HTTP
SERVER environment
Post by Uggla Henrik
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
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...