Php multi-user file read/write conflict solution

Source: Internet
Author: User
In fact, we will use flock to solve some problems when writing a file for multiple calls at the same time, so that only one user can write the file at the same time, while other users are waiting for the queue, below I will introduce how flock solves the problem of multiple users reading and writing files empty. so the general side... in fact, we will use flock to solve some problems when writing a file for multiple calls at the same time, so that only one user can write the file at the same time, while other users are waiting for the queue, below I will introduce how flock solves the problem of multiple users reading and writing files empty.

The general solution is as follows:

 

However, in PHP, flock does not seem to work so well. in the case of multiple concurrency, it seems that resources are frequently monopolized, and resources are not released immediately or are not released at all, resulting in deadlocks, in this way, the cpu usage of the server is very high, and sometimes the server is completely killed, as if this happens in many linux/unix systems.

Therefore, you must consider it carefully before using flock.

So there is no solution? In fact, this is not the case. if we use flock () properly, it is entirely possible to solve the deadlock problem. of course, if we do not consider using the flock () function, there will also be good solutions to solve our problems.

After my personal collection and summary, I have summarized the following solutions.

Solution 1: When locking an object, set a time-out period, which is roughly implemented as follows:

 

The timeout value is set to 1 ms. if the lock is not obtained within this time period, it is obtained repeatedly and directly until the file operation permission is obtained. of course, if the timeout limit is reached, you must exit immediately, let the lock be used by other processes.

Solution 2: Do not use the flock function. use temporary files to solve read/write conflicts.

The general principle is as follows:

1. Take the files to be updated into consideration a copy to our temporary file directory, Save the Last modification time of the file to a variable, and take a random copy for the temporary file, it is not easy to duplicate the file name.

2. After the temporary file is updated, check whether the last update time of the original file is consistent with the previously saved time.

3. If the last modification time is the same, rename the modified temporary file to the original file. to ensure that the file status is updated synchronously, clear the file status.

4. However, if the last modification time is the same as that previously saved, it indicates that the original file has been modified during this period. in this case, you need to delete the temporary file and return false, the file is being operated by other processes.

The general implementation code is as follows:

 

For the functions used in the above code, you need to describe them as follows:

1. rename (); rename a file or directory. This function is actually more like a music video in linux. it is very convenient to update the path or name of a file or directory.

However, when I test the code above in window, if the new file name already exists, a notice will be given, saying that the current file already exists, but it works well in linux.

2. clearstatcache (); clears the file status. php caches all file attributes to provide higher performance. However, when multiple processes delete or update files, php does not have time to update the file attributes in the cache, this function is used to clear the saved cache because the last update time is not the actual data.

Solution 3: Random read/write operations on files to reduce the possibility of concurrency.

This solution seems to be widely used to record user access logs.

Previously, we needed to define a random space. the larger the space, the lower the possibility of concurrency. Here we assume that the random read/write space is [1-500], then the distribution of our log files is log1 ~ To log500, data is randomly written to log1 ~ during each user access ~ Any file between log500.

At the same time, there are two processes that record logs. process A may be an updated log32 file, but what about process B? At this time, the update may be log399. you need to know that if you want the B process to operate log32, the probability is basically 1/500, which is about to be equal to zero.

To analyze access logs, we only need to merge these logs before analyzing them.

When this scheme is used to record the benefits of logs, the possibility of queuing process operations is relatively small, so that the process can quickly complete each operation.

Solution 4: Put all processes to be operated into a queue, and then put a service to complete file operations.

Each excluded process in the queue is equivalent to the first specific operation. Therefore, for the first time, our service only needs to obtain the Operation items from the queue, if there are a large number of file operation processes here, it doesn't matter, it's just after our queue. as long as you want to arrange the queue, it doesn't matter how long it will be.

The previous solutions have their own advantages. they may be classified into two categories:

1. Queuing (slow impact) is required, such as solution 1, solution 2, and solution 4.

2. No queue is required. (Fast impact) solution 3

When designing a cache system, we generally do not adopt solution 3, because the analysis program in solution 3 is not synchronized with the write program, and the analysis difficulty is not taken into account at the write time, just write the line. just think about it. for example, if we use the random file read/write method when updating a cache, it seems that many processes will be added when reading the cache, however, solution 1 and 2 are completely different. Although the write time needs to wait, the lock will be obtained repeatedly when it fails to be obtained, but it is very convenient to read the file, the purpose of adding a cache is to reduce the data read bottleneck and improve system performance.


Address:

Reprinted at will, but please attach the article address :-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.