我希望能够修改父 pid 中的数组元素。这是一些示例代码:
$arrayContainer = array(
array(
"id" => 1,
"name" => "Lenny"
),
array(
"id" => 2,
"name" => "Dudley"
),
array(
"id" => 3,
"name" => "Simon"
),
);
foreach ($arrayContainer as $key => $element) {
$pid = pcntl_fork();
if($pid == -1) {
// Something went wrong (handle errors here)
die("Could not fork!");
} elseif($pid == 0) {
$arrayContainer[$key]['size'] = 123;
$arrayContainer[$key]['fileName'] = 'somefile.txt';
// The child dies after a short while, becoming a zombie
exit();
} else {
// This part is only executed in the parent
}
}
因此,当此脚本结束时,我在子进程中编写的两个元素在 foreach 循环结束时不存在。我无法从孩子修改父 pid 中的数组。我明白为什么,但想不出一个能让我这样做的好解决方案。你能提出什么建议吗?全局变量什么的?