I guess the short answer would be NO, from my "research" I found there isn't a POSIX named semaphores implementation currently in PHP.
Bob Fanger posted an interesting workaround to convert strings to SysV semaphore keys, the downside is you have to implement this same algorithm in every code you need the semaphore.
What I ended up doing, though, was using flock()
on a 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 the extra lock file might seem as an overkill but you can only use LOCK_EX
on write mode, and sometimes I only needed to read the file.
note: flock()
works as an advisory file locking mechanism, if any other program tries to modify the file without calling flock()
it will succeed.