Ok, I know that there’s been similar questions on this site about this problem, but none of this questions and provided answers isn’t exactly what I need.
I’m building flat-file based CMS.
What if, for example:
-
2, 3, 10….. fwrite in appending mode requestes come to same php file “contact_form_messages_container.php” at the same time?
-
2, 3, 10….. fwrite in “w” mode requestes come to same php file which holds the simpley nubmer of specific page visits, again at the same time?
I know about flock() function, but it could happen two or more flock() requests comes on the same time… Does anyone knows solution to this problem? Only thing I have on my mind is usleep()-ing the script using while looop for some amount of time, until the target file becomes availibile, but I do not have idea if it works, where and how to perform this?
Does anyone have practical expirience with this issue?
Thanks in advance!
The
flock()function is designed to handle multiple concurrent readers and writers for file operations; by defaultflock()may suspend a process until a compatible lock can be obtained (i.e. shared or exclusive). Once obtained, a lock can later be released to allow other processes to operate on the file; locks are released implicitly when the file is closed or the process ends.Unless your files are on NFS, I highly doubt you will ever run into a situation whereby two conflicting locks would be given simultaneously.
The following illustrates a basic example of using
flock():Using the
LOCK_NBflag together withLOCK_EXorLOCK_SHwill prevent the process from being suspended; if the call returnsfalsea third parameter can be passed to determine whether the process would have been suspended (not supported on Windows).