The supplied documentation is vague, ambiguous and lacking, and the user comments contain erroneous information! The flock function follows the semantics of the Unix system call bearing the same name.
It is commonly called a reader lock. Only a single process may possess an exclusive lock to a given file at a time. If however, you call flock on a file on which you possess the lock, it will try to change it.
When a file is closed the lock will be released by the system anyway, even if PHP doesn't do it explicitly anymore since 5. The lock was kept alive until apache recycled those processes. This lack of proper clean up basically makes flock completely unreliable. Regarding the change in PHP 5. From 5. But note, that the operating system releases the lock automatically when the file is closed.
Actually, there is no use of the while loop with the usleep. It is almose guaranteed that the file will be locked, unless the script times out or something. Consider these two scripts: 1st one is ran, and the second one is ran 5 seconds after the first. As soon as the first script finishes, the second one will acquire the lock and finish the execution. I have noticed that if you change the value of your fopen ressource, the lock is working no longer..
But only moves the pointer to the begining of the file. Indeed, flock will not work reliably when the underlying filesystem is NFS. The proper way to perform file locking, in this case, would be to use PHP's link function. In this context, a symbolic link exists, regardless of where its points to. The solution for per- forming atomic file locking using a lockfile is to create a unique file on the same fs e. If link returns 0, the lock is successful. Otherwise, use stat 2 on the unique file to check if its link count has increased to 2, in which case the lock is also successful.
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.
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. Here's a handy class to allow retrying a write with flock a set number of times.
The maximum number of bytes read is specified by the length parameter. Safe for reading binary data. Submit a Pull Request Report a Bug. Changelog Version Description 8. And im "working" on this problem bug? This seems to be meant literally. PHP 5. Both checking for '' and false to detect closed connection is probably smart, as this "bug"? It is not obvious from the docs or notes Returns true on success or false on failure.
Submit a Pull Request Report a Bug. Return Values Returns true on success or false on failure. I've successfully used this approach on Linux with PHP 4. A way to have a non-blocking pipe reader is to check first if the pipe exists.
If so, then read from the pipe, otherwise do other stuff.
0コメント