|
Posted by J.O. Aho on 12/29/05 02:31
Leszek wrote:
> Included -yes but I need to make sure that prog1.php is done before
> prog2.php starts
As everything is executed in sequel, the line thats is before another, will
first be done before the next line is executed
so we have two scripts,
--- script1.php ---
<?PHP
for($i=0;$i<10000;$i++) {
x++;
}
echo $x."\n";
for($i=0;$i<10000;$i++) {
x++;
}
echo $x."\n";
echo "script1 finished!!!\n";
?>
--- eof ---
--- script1.php ---
<?PHP
echo "script2 finished!!!\n";
?>
--- eof ---
Without any deep study we can see that script1.php takes longer time to
execute than what script2.php
We then have the main script
--- main.php ---
<?PHP
include('script1.php');
include('script2.php');
?>
--- eof ---
No matter which computer, what type of CPU it has, as long as it has a working
version of PHP that supports include(), echo() and for() (that should be all
released versions), you will get the same result
--- output of executing main.php ---
10000
20000
script1 finished!!!
script2 finished!!!
--- eof ---
If you want to execute something at the same time, you need to fork the
process and for that you need pcntl_fork()
and to make things to do as you don't want, that the results from the fastest
script run will come first, then you need to modify the main.php to look
something like this
--- forkedmain.php
if (($pid = pcntl_fork()) == -1) {
die("could not fork");
} else if ($pid) {
include('script1.php);
} else {
include('script1.php);
}
?>
--- eof ---
This could result in an output that looks more like
--- output of executing main.php ---
script2 finished!!!
10000
20000
script1 finished!!!
--- eof ---
So to be sure that your first include will be executed before your second
include, _DON'T_ use pcntl_fork().
//Aho
Navigation:
[Reply to this message]
|