Created
August 27, 2014 06:50
-
-
Save user3415653/feae1704cdc3c2b9c806 to your computer and use it in GitHub Desktop.
PHP Semaphore example
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 | |
| //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