View Full Version : How to run the same process multiple times at the same time? (Forking??)

04-26-2005, 06:35 AM

I have a simple script that gets data from a mysql database and runs a process for each item selected from the database using the while command. I wanted to find out if it is possible to run a section of the script multiple times at the same time with control of how many children processes it runs at once. I belive it's called forking but I am still new to php and have no idea on where to begin or if it's easily possible.

Here's my current script. It's still a work in progress but should give you an idea of what I'm trying to accomplish.

//Database Connection
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM su";


while ($j < $num) {

//I want this section to run multiple times at the same time

$fsock = fsockopen("$address", 80, $errno, $errstr, 5);
if ($errno == "0") {
$query = "UPDATE su SET success = success+1, lr = 'up' WHERE address = '$address'";

if ($lr == "dn") {
$subject="Site back online";
$msg="Your Server Appears To Be Back Online. The address $address is now reachable.";
$mailheaders="From: Server Report";
mail ($email, $subject, $msg, $mailheaders);

else {
$query = "UPDATE su SET failed = failed+1, lr = 'dn' WHERE address = '$address'";
if ($lr == "up") {
$subject="Site not responding";
$msg="Your Server Appears To Be Offline. The address $address was unreachable. You will recive a e-mail when the site is again reachable.";
$mailheaders="From: Server Report";
mail ($email, $subject, $msg, $mailheaders);

//End PHP fork process?



If you have any suggestions or a good tutorial on this subject, that would be a great help!

Thank you.

04-26-2005, 12:56 PM
Helllo Friend!
H R U.
I hope this will help u a little bit.
PLZ c this 'pcntl_fork' in the PHP Manual.
Aamir. :thumbsup:

04-26-2005, 04:53 PM
I've already looked at pcntl_fork but I am not 100% sure what to do with it. How would I go about controling them and would it work with my current code? Will the primary script execute the code with the data from the database with the while statment?

Thanks for the help!

Velox Letum
04-26-2005, 08:22 PM
In the PCNTL functions (http://php.he.net/manual/en/ref.pcntl.php) page I found a few examples in the user submitted notes at the bottom which might help you. Without more time to experiment and read in depth though I cannot help you further.

04-26-2005, 11:40 PM
A few of the examples helped me a bit but I am having trouble understanding how to use the data from the database to run each process. There were no examples there for that. If anybody has some sample code of forking using a database, please post part of it.

Thanks for th help!

04-27-2005, 02:08 AM
I think i'm going to run multiple copies of the script at diffrent time intervals using a where statment to pick the id's 1-50 and so on. Mabyee later on when I know more about php and forking processes I will re-write the script. Thanks for all the help!