16.1.4 Duplication of resources when forkingThis is NOT the latest copy of this book; click here for the latest version.
When you fork a child from your PHP script, that child inherits all variables and resources that were opened by its parent. In the case of files, both parent and child inherit the same file descriptors which means that you can write to the same file from several processes if you want to. In practice this is undesirable as you will get intermingled lines of text from the different process, however there's no harm seeing how it's done just in case!
This next example opens the file "forktest" and has each child write two lines into it:
<?php
$fp = fopen("forktest", "a");
for ($i = 1; $i <= 5; ++$i) {
$pid = pcntl_fork();
if (!$pid) {
fwrite($fp, "In child $i\n");
sleep(1);
fwrite($fp, "Still in child $i\n");
exit($i);
}
} ?>
Once that script has executed, the forktest file should be filled with this text:
In child 1
In child 2
In child 3
In child 4
In child 5
Still in child 1
Still in child 2
Still in child 3
Still in child 4
Still in child 5
Similarly you can have all your children connect to a MySQL database just by connecting once. First, create a table in your phpdb database using the following command:
CREATE TABLE forktest (StringVal CHAR(255));
Now, save this next script as forkdb.php and run it:
<?php
mysql_pconnect("localhost", "phpuser", "alm65z");
mysql_select_db("phpdb");
mysql_query("INSERT INTO forktest VALUES ('Parent about to fork children');");
for ($i = 1; $i <= 5; ++$i) {
$pid = pcntl_fork();
if (!$pid) {
mysql_query("INSERT INTO forktest VALUES ('In child $i');");
exit($i);
}
}
mysql_query("INSERT INTO forktest VALUES ('Parent exiting...');"); ?>
After the script completes, you should have seven rows in the table charting the progress of your script, all by connecting just once. Appearances are deceiving, however - if you try running the above script yourself you will find that PHP actually makes several connections to MySQL. This is where you start getting into the realms of the unknown, by which I mean that sharing resources across processes is a hairy business best left alone if possible.
The solution here is to have children open their own resources up. This makes your code clearer, avoids problems with processes treading on each other's toes, and also means you will be less likely to encounter cross-platform problems with your code.
|
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|