note 82521 added to function.flock

From: Date: Tue, 15 Apr 2008 00:59:59 +0000
Subject: note 82521 added to function.flock
Groups: php.notes 
Request: Send a blank email to php-notes+get-138314@lists.php.net to get a copy of this message
I just want to add a note about making atomic lock on NFS, there is only two ways: - 1 (the most robust but the most complicate) - It's to use link() to create a hard link to a file you want to lock (on the same FS of course). (On most NFS implementations, Link() is atomic) Once you created a hard link (not a symbolic link), with a unique randomly generated name, call stat() on it and count the number of link (nlink), if there is only 2 then the file is locked. If there is more than two you have to unlink() the link you just created and create a new one with a new unique name (else NFS will use its cache and stat will return wrong data) then call stat() on the new link and test the number of links again, repeat this operation until you get the lock. You have to use usleep() between the link() attempts with a fixed + random sleep value to avoid dead lock situations (link() and unlink() may be atomic but not instantaneous) Also note than when you unlink a file through NFS, if NFS think that the file is still in use, it will create a .nfs link to this file until it realizes the file is no longer in use... A wrong timing could generate thousands of those files and a deadlock situation. Because of this when a deadlock situation occurs or if your stat() command returns a very high number of links, you have to look for .nfs file in the same directory you created your links and unlink all the .nfs file you find (sometimes NFS take its time to remove them) - 2 (the simplest) - the second method is to use a lock server and lock daemons on each client that will forward lock request to the server... (this is more dangerous than the first method because the daemons may be killed...) Here is for reference the function I created to make atomic locks through NFS (this function is in production since at least 4 years now), it's just for reference because it uses many external functions to do its job but you can see the principle: http://pastey.net/85793 ---- Server IP: 217.174.223.124 Probable Submitter: 88.164.134.95 ---- Manual Page -- http://www.php.net/manual/en/function.flock.php Edit -- https://master.php.net/note/edit/82521 Del: integrated -- https://master.php.net/note/delete/82521/integrated Del: useless -- https://master.php.net/note/delete/82521/useless Del: bad code -- https://master.php.net/note/delete/82521/bad+code Del: spam -- https://master.php.net/note/delete/82521/spam Del: non-english -- https://master.php.net/note/delete/82521/non-english Del: in docs -- https://master.php.net/note/delete/82521/in+docs Del: other reasons-- https://master.php.net/note/delete/82521 Reject -- https://master.php.net/note/reject/82521 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138314) next »