Bluehost.com Web Hosting $6.95

pcntl_fork apache and system

This is a discussion on pcntl_fork apache and system within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello, I'm trying to run a system call('ls' in this simple case) that is taking quite a long. ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-03-2006
David
 
Posts: n/a
Default pcntl_fork apache and system

Hello,
I'm trying to run a system call('ls' in this simple case) that is taking
quite a long. I would like to fork the system call into a child process
and waits display the output of the call while it is running.

Here is a piece of code:

$pid = pcntl_fork();

for ($i = 1; $i <= 5; ++$i) {
$pid = pcntl_fork();

if (!$pid) {
$query = "ls /tmp";
system ($query,$ret_val);
print "In child $i\n";
exit;
}
}

How can i make this script dynamic in my web page. Meaning how can i
print each loop while the child process is running in Apache and
redirect to STDOUT in the webpage???
Reply With Quote
  #2 (permalink)  
Old 07-03-2006
-Lost
 
Posts: n/a
Default Re: pcntl_fork apache and system

"David" <david@nospam.com> wrote in message news:44a935e9$0$9020$626a54ce@news.free.fr...

> How can i make this script dynamic in my web page. Meaning how can i print each loop
> while the child process is running in Apache and redirect to STDOUT in the webpage???


The methods flush and ob_flush.

-Lost


Reply With Quote
  #3 (permalink)  
Old 07-03-2006
David
 
Posts: n/a
Default Re: pcntl_fork apache and system

Well i did try both but doesn't seem to do what i want:

This does only display the first line that has been found, the rest is
not sent to the browser..!!!

$query ="find ./ -iname '*.txt' ";
echo "<b>Running query:<br>$query</B><br><br>";
ob_start();
system($query,$return_val);
ob_flush();
flush();

Any idea ???

-Lost a écrit :
> "David" <david@nospam.com> wrote in message news:44a935e9$0$9020$626a54ce@news.free.fr...
>
>> How can i make this script dynamic in my web page. Meaning how can i print each loop
>> while the child process is running in Apache and redirect to STDOUT in the webpage???

>
> The methods flush and ob_flush.
>
> -Lost
>
>

Reply With Quote
  #4 (permalink)  
Old 07-03-2006
-Lost
 
Posts: n/a
Default Re: pcntl_fork apache and system

"David" <david@nospam.com> wrote in message news:44a93ca2$0$21817$626a54ce@news.free.fr...

> Well i did try both but doesn't seem to do what i want:
>
> This does only display the first line that has been found, the rest is not sent to the
> browser..!!!
>
> $query ="find ./ -iname '*.txt' ";
> echo "<b>Running query:<br>$query</B><br><br>";
> ob_start();
> system($query,$return_val);
> ob_flush();
> flush();
>
> Any idea ???


None, sorry! I am not on a *nix machine right now, so I cannot even test any code.
(Obviously, the Windows version tells me pcntl_fork is not even a valid function.)

-Lost


Reply With Quote
  #5 (permalink)  
Old 07-03-2006
David
 
Posts: n/a
Default Re: pcntl_fork apache and system

Ok no problem,
Let me know if you figure out..!!!

-Lost a écrit :
> "David" <david@nospam.com> wrote in message news:44a93ca2$0$21817$626a54ce@news.free.fr...
>
>> Well i did try both but doesn't seem to do what i want:
>>
>> This does only display the first line that has been found, the rest is not sent to the
>> browser..!!!
>>
>> $query ="find ./ -iname '*.txt' ";
>> echo "<b>Running query:<br>$query</B><br><br>";
>> ob_start();
>> system($query,$return_val);
>> ob_flush();
>> flush();
>>
>> Any idea ???

>
> None, sorry! I am not on a *nix machine right now, so I cannot even test any code.
> (Obviously, the Windows version tells me pcntl_fork is not even a valid function.)
>
> -Lost
>
>

Reply With Quote
  #6 (permalink)  
Old 07-03-2006
Colin McKinnon
 
Posts: n/a
Default Re: pcntl_fork apache and system

David wrote:

> Here is a piece of code:
>
> $pid = pcntl_fork();

<snip>
>
> How can i make this script dynamic in my web page. Meaning how can i
> print each loop while the child process is running in Apache and
> redirect to STDOUT in the webpage???


I can't help thinking that forking is the wrong place to start from - as
you've realised you suddenly get away from the simple I/O model.

The next problem is that (IIRC) ls first reads in the directory then
generates output - so the last directory entry will be ready pretty soon
after the first.

Another consideration is that if 'ls' is taking a long time, you've got
admin problems you need to solve.

-Lost's suggestion of using flush is a good one (don't bother forking - just
use popen) - but depending on the furniture you put about it, your browser
may have problems rendering the incomplete page - if you are using a fancy
layout you might consider embedding the output in an iframe, or using
javascript document.writes to insert it afterwards.

HTH

C.
Reply With Quote
  #7 (permalink)  
Old 07-04-2006
David
 
Posts: n/a
Default Re: pcntl_fork apache and system

Thanks, that's exactly what i have done. Using the popen function did
the work. Now the output is passed to the web browser immedialty after
launching.

function show_buffer($query)
{
echo "<pre>";
$handle = popen($query, 'r');
while(!feof($handle)) {
$buffer = fgets($handle);
echo "$buffer";
ob_flush();
flush();
}
pclose($handle);
echo "</pre>";
}

$query = "ls /tmp/*.txt";
show_buffer($query);



Colin McKinnon a écrit :
> David wrote:
>
>> Here is a piece of code:
>>
>> $pid = pcntl_fork();

> <snip>
>> How can i make this script dynamic in my web page. Meaning how can i
>> print each loop while the child process is running in Apache and
>> redirect to STDOUT in the webpage???

>
> I can't help thinking that forking is the wrong place to start from - as
> you've realised you suddenly get away from the simple I/O model.
>
> The next problem is that (IIRC) ls first reads in the directory then
> generates output - so the last directory entry will be ready pretty soon
> after the first.
>
> Another consideration is that if 'ls' is taking a long time, you've got
> admin problems you need to solve.
>
> -Lost's suggestion of using flush is a good one (don't bother forking - just
> use popen) - but depending on the furniture you put about it, your browser
> may have problems rendering the incomplete page - if you are using a fancy
> layout you might consider embedding the output in an iframe, or using
> javascript document.writes to insert it afterwards.
>
> HTH
>
> C.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:44 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0