Skip to content

Instantly share code, notes, and snippets.

@yasudacloud
Created September 2, 2022 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasudacloud/86cac174249561c1d22d10dbcff99824 to your computer and use it in GitHub Desktop.
Save yasudacloud/86cac174249561c1d22d10dbcff99824 to your computer and use it in GitHub Desktop.
<?php
$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$pid = pcntl_fork();
const EXIT_CODE = "-1\n";
if ($pid == -1) {
die('フォークできません');
} else if ($pid) {
echo "@start parent\n";
while (($buffer = fgets($sockets[0], 4096)) !== false) {
if ($buffer === EXIT_CODE) {
break;
}
echo $buffer;
}
fclose($sockets[1]);
echo "@end parent\n";
} else {
echo "@start child\n";
for ($i = 1; $i <= 10; $i++) {
fwrite($sockets[1], "${i}回目\n");
sleep(1);
}
fwrite($sockets[1], EXIT_CODE);
fclose($sockets[0]);
echo "@end child\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment