Skip to content

Instantly share code, notes, and snippets.

@andrewmackrodt
Created October 16, 2014 10:13
Show Gist options
  • Select an option

  • Save andrewmackrodt/8591628d678321e6a920 to your computer and use it in GitHub Desktop.

Select an option

Save andrewmackrodt/8591628d678321e6a920 to your computer and use it in GitHub Desktop.
<?php
$shm = shm_attach(ftok(__FILE__, 'p'), 1024, 0640);
shm_put_var($shm, 0, '');
$sem = sem_get(ftok(__FILE__, 'p'), 1, 0640, 1);
for ($i = 0; $i < 10; $i++) {
if (0 === ($pid = pcntl_fork())) {
$pid = posix_getpid();
echo "Child $pid\n";
// indicate to the parent that the child process has completed
sem_acquire($sem);
shm_put_var($shm, 0, str_replace("/$pid/", '', shm_get_var($shm, 0)));
sem_release($sem);
// kill the fork to avoid shutdown handlers
posix_kill($pid, SIGKILL);
}
// indicate to the parent that a child process has started
sem_acquire($sem);
shm_put_var($shm, 0, shm_get_var($shm, 0) . "/$pid/");
sem_release($sem);
}
while ('' !== ($pid = shm_get_var($shm, 0))) {
echo "$pid\n";
usleep(1);
}
sem_remove($sem);
shm_remove($shm);
echo "Done\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment