Using PHP SYS V Semaphores with Other Applications
| From: | Mike Papper | Date: | Tue, 25 Mar 2003 03:18:30 +0000 |
| Subject: | Using PHP SYS V Semaphores with Other Applications | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-140986@lists.php.net to get a copy of this message | ||
I would like to synchronize between various PHP pages as well as some C++
applications. I thought I could use the SYS V semaphores in PHP along with
the C semaphore library to do this. I wrap this semaphore into a Mutex
class (one for PHP and one for C++) and use the semaphore strictly for
mutual exclusive access. It appears that the way PHP manipulates semaphores
is comepltly backwards with respect to the C library...
There appears to be some issues, namely:
1) when the semaphore is created by PHP (using sem_get) it will create a
semaphore set with 3 semaphores in it. When created with C code I can
choose how many items are in the set (why not use 1 item).
2) In PHP it initializes the 3 items in the semaphore set all with a 0
value. In C each item is initialized with a 0 value but the library allows
me to set them to 1 to use the semaphore for locking (in a thread-safe
manner).
The problems:
1) why does PHP need to a semaphore set with 3 items?
2) In C++ I initialize the sempahore items to the value 1. This means the
resource is available. When someone acquires the resource this "value" is
decremented. When it reaches zero the count is 0 and the semaphores is
unavailable. When the C code releases the semaphore, it increments this
value and ther semaphore becomes available.
In C the function call you can make to acquire the semaphore resource is
"semctl" specifiying "-1" as the command. This will block until the value
is greater than 0 (and it is initially greater than 0 when no other
process/thread has acquired the semaphore).
NOTE: I am NOT having 1 process increment the semaphore and 1 process
decrement it (as explained in the PHP documentation, the semaphore module
cannot do this). I am using it stricly for mutual exclusion. If a process
acquires the semaphore (decrements the count from 1 to 0) it must release
(increment the count from 0 to 1).
THE BIG PROBLEM is that PHP uses the oppostie semantics as the C library.
In PHP, when one acquires the sempahore it increases the value count (from
0 to 1) - it starts out at 0 in PHP. When PHP releases it decrements the
value from 1 to 0. It seems to only do this to the 3rd semaphore item in
the semaphore set.
I feel that the implementation of semaphores in PHP has 2 major issues:
1) it should not have to create 3 semaphores in the set
2) it should use the C type sematics of decrementing the count to acquire
the reosurce and incrementing to release it.
OTHERWISE the current behaviour of the semaphore module means that we
cannot share the same semaphore between PHP and C.
Question: why did PHP implement it this way?
Will someone fix it so that it is compatable with the rest of the world
(aka all C programs running on the same machine)?
Or perhaps I am missing something here.
--
Mike Papper