私は、私の様々なPHPのcliプログラムで自分のニーズに完全に動作するこのコードブロックを持っています。ただし、時には子供がゾンビになることを除けば。PHPフォーク:ゾンビになると子供を殺す
私の質問は、子供が5分間発言するかどうかを確認するコードをどこに置くのか、そしてそれが長くなるとそれを殺すことですか?
私はそれを殺すためにposix_killについて知っており、それをどのように追跡するのですか? There are examples of taskmanagers here。
これらの新機能をコードに組み込む方法がわかりません。私がしようとするたびに、私はちょうど混乱に陥る。おそらく、フォークを知っている人が自分のコードを修正できますか?
すべてのerror_logを無視します。実行時に何が起きているのが好きです。すべての
public function __construct($data) {
//Keep track of all of the children processes
$this->children = Array();
//Specify the maximum number of child processes to fork at any given time
$this->max_children = 5;
}
private function process()
{
foreach ($collection as $stuff)
{
//FORK THE PROCESS
$pid = pcntl_fork();
//Something went wrong
if($pid == -1)
{
error_log ("could not fork");
die();
}
//PARENT PROCESS
if($pid)
{
//error_log ("Parent: forked " . $pid);
$this->children[] = $pid;
}
//CHILD PROCESS
else
{
// Do stuff here
exit(); //Exit the child thread so it doesn't continue to process the data
}
//COLLECT ALL OF THE CHILDREN AS THEY FINISH
while(($c = pcntl_wait($status, WNOHANG OR WUNTRACED)) > 0)
{
//error_log ("Collected Child - " . $c);
$this->remove_thread($this->children, $c);
error_log ("children left: " . count($this->children));
}
//WAIT FOR A CHILD TO FINISH IF MAXIMUM PROCESSES IS EXCEEDED
if(sizeof($this->children) > $this->max_children)
{
//error_log ("Maximum children exceeded. Waiting...");
if(($c = pcntl_wait($status, WUNTRACED)) > 0)
{
//error_log ("Waited for Child - " . $c);
$this->remove_thread($this->children, $c);
error_log ("children left: " . count($this->children));
}
}
}
//COLLECT ALL OF THE CHILDREN PROCESSES BEFORE PROCEEDING
while(($c = pcntl_wait($status, WUNTRACED)) > 0){
//error_log ("Child Finished - " . $c);
$this->remove_thread($this->children, $c);
error_log ("children left: " . count($this->children));
}
}
//Function to remove elements from an array
private function remove_thread(&$Array, $Element)
{
for($i = 0; $i < sizeof($Array); $i++)
{
//Found the element to remove
if($Array[$i] == $Element){
unset($Array[$i]);
$Array = array_values($Array);
break;
}
}
}
素晴らしいタイトル... –
子供たちは...彼らはまだ生きているではないので、ゾンビになっ –
@Ignacio - と言う、私はカール経由でプロキシをチェックしていますインスタンスがあります。 Curlが解体してしまったために子供が反応しなくなると、1000代の代理人を早急にチェックしていれば、すべての子どもがゾンビ化されます。それは、私がそれらを殺して新しい子供を作ることができるように、彼らがどれくらいの期間走っているのかを知る必要がある1つの事例です。 – PaulM