PHP called semaphores?

I look around, but I can not find any implementation of POSIX semaphores for PHP. The only thing I see is SysV semaphores.

(2 questions)

Is there any way to access named semaphores from PHP at the moment?

Are there any plans for future releases of PHP?

+4
source share
2 answers

I assume the short answer will be NO , from my "research" I found that there is currently no implementation of POSIX semaphores in PHP.

Bob Fanger has published an interesting workaround for converting strings to SysV semaphore keys, the disadvantage is that you have to implement the same algorithm in every code you need a semaphore.

What I eventually did was use flock() in the lock file.

Php

 # open an exclusive lock $lock = fopen('/path/to/file.lock', 'w'); flock($lock, LOCK_EX); # edit a file $f = fopen ('/path/to/file.txt', 'a'); fwrite($f, "append through PHP\n"); fclose($f); # unlock flock($lock, LOCK_UN); fclose($lock); 

Perl

 use Fcntl qw(:flock); # open an exclusive lock open LOCK, '>/path/to/file.lock'; flock LOCK, LOCK_EX; # edit a file open FILE, '>>/path/to/file.txt'; print FILE "append through PERL\n"; close FILE; # unlock flock LOCK, LOCK_UN; close LOCK; 

I know that an additional lock file may seem redundant, but you can use LOCK_EX only in write mode, and sometimes I only need to read the file.

Note: flock() acts as a mechanism to lock the advisory file; if any other program tries to modify the file without calling flock() , it will succeed.

+2
source

Here is my implementation of "named" semaphore, but I'm not sure if you are looking for a simple converter for int int.

 /** * Generate a semaphore integer from a string/key * * @param string $identifier * @return int */ function sem_key($identifier) { $md5 = md5($identifier); $key = 0; for ($i = 0; $i < 32; $i++) { $key += ord($md5{$i}) * $i; } return $key; } 
+3
source

All Articles