Ok, I found a solution to my problem. To clear some confusion, I have two applications that are running in the background and writing to a buffer. When a client connects to the server, they connect to both buffers and start receiving data. So what I did was to create a new process for each channel. When the client connects to the server, two processes are created and assigned to the client. Each process is connected to a buffer and starts sending data to the client.
private function startProc($client) {
$this->output("Start a client process");
for ($i = 0; $i < 2; ++$i) {
// Create new process.
$pid = pcntl_fork();
switch ($pid) {
case -1: // Failed to create new process.
$this->output("Failed to create new process!");
break;
case 0: // Child process created, execute code...
switch ($i) {
case 0:
$this->output("Connect to AppOne data pipe...");
$this->launchAppOneProc($client);
break;
case 1:
$this->output("Connect to AppTwo data pipe...");
$this->launchAppTwoProc($client);
break;
}
default: // Back to parent.
switch ($i) {
case 0:
$client->setAppOnePid($pid);
break;
case 1:
$client->setAppTwoPid($pid);
break;
}
}
}
}
I know I can use an array to store the process IDs, etc. But for now, this works. Once the client disconnects, each process for the client that just left has to be terminated as such:
posix_kill($client->getAppOnePid(), SIGTERM);
posix_kill($client->getAppTwoPid(), SIGTERM);
I hope this make sense and helps anyone who runs into the same problem.