Question

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:

  1. 2, 3, 10..... fwrite in appending mode requestes come to same php file "contact_form_messages_container.php" at the same time?

  2. 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!

Was it helpful?

Solution

The flock() function is designed to handle multiple concurrent readers and writers for file operations; by default flock() 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():

// open the file (take care to not use "w" mode)
$f = fopen('file.txt', 'r+');
// obtain an exlusive lock (may suspend the process)
if (flock($f, LOCK_EX)) {
    // this process now holds the only exclusive lock

    // make changes to the file

    // release the lock
    flock($f, LOCK_UN);
}
// don't perform any write operation on $f here
fclose($f);

Using the LOCK_NB flag together with LOCK_EX or LOCK_SH will prevent the process from being suspended; if the call returns false a third parameter can be passed to determine whether the process would have been suspended (not supported on Windows).

if (false === flock($f, LOCK_EX | LOCK_NB, $wouldblock)) {
    if ($wouldblock) {
        // the lock could not be obtained without suspending the process
    } else {
        // the lock could not be obtained due to an error
    }
}

OTHER TIPS

Fork the writing operation and put it into a while loop with a sleep while file is locked. As long as your application doesn't depend on making file system calls chronologically it should work.

This does however open for race conditions if your application depends on writing operations to happen in order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top