Skip to content

Instantly share code, notes, and snippets.

@erubboli
Created August 19, 2010 17:14
Show Gist options
  • Save erubboli/538395 to your computer and use it in GitHub Desktop.
Save erubboli/538395 to your computer and use it in GitHub Desktop.
<?php
$id=1;
$shared1 = shm_attach(1);
$sem = sem_get(1,1);
shm_put_var($shared1,1,$id);
shm_put_var($shared1,2,$sem);
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if (!$pid) {
while(true){
$s1 = shm_attach(1);
sem_acquire($sem);
$idx = shm_get_var($s1,1);
print "P1: hello (".($idx++).") ";
shm_put_var($s1,1,$idx);
sem_release($sem);
usleep(25600);
}
}
sleep(1);
$pid2 = pcntl_fork();
if($pid2 == -1){
die('could not fork');
}else if(!$pid2){
while(true){
$s1 = shm_attach(1);
sem_acquire($sem);
$idx = shm_get_var($s1,1);
print "P2: hello (".($idx++).") ";
shm_put_var($s1,1,$idx);
sem_release($sem);
usleep(25600);
}
}
if($pid || $pid2){
pcntl_wait($status);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment