Created
October 16, 2014 10:13
-
-
Save andrewmackrodt/8591628d678321e6a920 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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