|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-06-25 18:20 UTC] ein at anti-logic dot com
Description:
------------
It is impossible to duplicate flock functionality with the sem controls due to sem_get's and sem_aquire's inability to ensure exclusive access.
For example with flock, if we create a non exclusive (shared) lock, followed by an exclusive lock, it will block on the second lock creation. This is the expected behaviour, and allows us to ensure that there is exclusive access to the file.
Reproduce code:
---------------
$fp = sem_get(fileinode('lock_file', 100, 666);
sem_acquire($fp);
echo "one lock is in place, we should Block on the second lock request";
$fp2 = sem_get(fileinode('lock_file', 1, 666);
sem_acquire($fp2);
echo "but we have not";
$fp3 = fopen('lock_file', 'w');
flock($fp3, LOCK_SH);
echo "one flock is in place, we should block on the next lock request";
$fp4 = fopen('lock_file', 'w');
flock($fp4, LOCK_EX);
echo "we will never see this text due to proper indefinite blocking";
Expected result:
----------------
sem_aquire should block on the second lock creation.
Actual result:
--------------
It will not block, nor will sem_get or sem_aquire return an error.
I believe the confusion may be caused by this line in the documentation, "Actually this value is set only if the process finds it is the only process currently attached to the semaphore." However, that is reffering only to the perm field, and not max_aquire. Either the documentation should be updated to reflect the (broken) implementation, or the sem_aquire/sem_get functions should be modified to ensure an exclusive lock request (max_aquire=1) will block when an existing lock is in place.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 30 09:00:02 2025 UTC |
>$fp = sem_get(fileinode('lock_file', 100, 666); Why would it block if you set max_acquire to 100. Change it to 1 and it will block.