/** * fork process * * @param callable $callable * @param array $args * @param string $tag * @return int $pid forked PID of forked child process * @throws RuntimeException */ public function fork($callable, $args = array(), $tag = null, Snidel_Token $token = null) { $token = $token ? $token : $this->token; if (!is_array($args)) { $args = array($args); } $pid = pcntl_fork(); if (-1 === $pid) { $message = 'could not fork a new process'; $this->error($message); throw new RuntimeException($message); } elseif ($pid) { // parent $this->info('created child process. pid: ' . $pid); $this->childPids[] = $pid; if ($tag !== null) { $this->tagsToPids[$tag][] = $pid; } } else { // child foreach ($this->signals as $sig) { pcntl_signal($sig, SIG_DFL, true); } $this->info('waiting for the token to come around.'); if ($token->accept()) { $this->info('started the function.'); $ret = call_user_func_array($callable, $args); $this->info('completed the function.'); $data = new Snidel_Data(getmypid()); try { $data->write($ret); } catch (RuntimeException $e) { throw $e; } $token->back(); } $this->_exit(); } return $pid; }
/** * @test */ public function writeAndRead() { $data = new Snidel_Data(getmypid()); $data->write('foo'); $this->assertSame($data->readAndDelete(), 'foo'); }