Question

I want to write a php script to run on background. that needs pcntl_fork. I use this function to fork a child :

function launchchild($programexe,$programvars) 
{ 
//foreach ($tasks as $key => $v){ 
switch ($pid = pcntl_fork()) { 
  case -1: 
     // @fail 
     die('Fork failed'); 
     break; 

  case 0: 
     // @child: Include() misbehaving code here 
     pcntl_exec($programexe,$programvars);
     break; 

  default: 
     // @parent 
     //pcntl_waitpid($pid, $status); 
     break; 
} 

//print "Done! :^)\\n\\n"; 
}

i call it with a snippet like below so much in a minute:

$parameters = array('/Applications/XAMPP/xamppfiles/htdocs/ping/senario2/master/gather_checks.php', $serialize_triagger, $server_list[$server_choose]);
launchchild('/Applications/XAMPP/xamppfiles/bin/php',$parameters); // fork a child for parallel processing.

so, it works gracefully only for some minute. i checked the process with 'ps -ax | grep php'. i saw there is so much process that never end until the code will give 35 error that means we have not more memory to run that! this is the result of 'ps -ax | grep php'

ps -ax | grep php
12020 ttys001    0:00.07 /Applications/XAMPP/xamppfiles/bin/php /Applications/XAMPP/xamppfiles/htdocs/ping/senario2/master/main.php
12021 ttys001    0:00.00 (php-5.3.1)
12022 ttys001    0:00.00 (php-5.3.1)
12025 ttys001    0:00.00 (php-5.3.1)
12028 ttys001    0:00.00 (php-5.3.1)
12037 ttys001    0:00.00 (php-5.3.1)
12040 ttys001    0:00.00 (php-5.3.1)
12043 ttys001    0:00.00 (php-5.3.1)

when i uncomment the //pcntl_waitpid($pid, $status); it work like a charm without any problem, ever. but i don't want the parent wait for child work. any thing can be helpful.

Was it helpful?

Solution

You're launching another program/script, so you don't need any forking, just use system() and screen for taking terminal (idk if & works)

OTHER TIPS

PHP's PCNTL functions, like pcntl_fork, should not be used from a web server, as the child processes do not end up getting cleaned up properly. (As you've no doubt noticed.) These functions are primarily intended for use in standalone PHP daemons, and in special cases which don't apply here.

Use the higher-level process control functions like proc_open() instead. If you need to run a process in the background, run it through a shell with &.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top