Skip to content

Instantly share code, notes, and snippets.

@user3415653
Created August 27, 2014 06:50
Show Gist options
  • Select an option

  • Save user3415653/feae1704cdc3c2b9c806 to your computer and use it in GitHub Desktop.

Select an option

Save user3415653/feae1704cdc3c2b9c806 to your computer and use it in GitHub Desktop.
PHP Semaphore example
<?php
//Semaphore properties
$key = 123456;
$max = 1;
$permissions = 0666;
$autoRelease = 1;
//Open a new or get an existing semaphore
$semaphore = sem_get($key, $max, $permissions, $autoRelease);
if (!$semaphore) {
echo "Failed on sem_get().\n";
exit;
}
//Try to aquire the semaphore.
for ($i = 0; $i < 2; $i++) {
echo "\nAttempting to acquire semaphore...\n";
sem_acquire($semaphore);
echo "Aquired.\n";
echo "Enter some text: ";
$handler = fopen("php://stdin", "r");
$text = fgets($handler);
fclose($handler);
sem_release($semaphore);
echo "Got: $text \n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment