PHP Data function flock

Source: Internet
Author: User
Tags flock php write
Php file function flock ???? In the case of concurrency, how does PHP write files? In fact, this problem is not just a problem facing PHP. Both threads and processes may encounter shared resource write conflicts when writing concurrently. During software development, write conflicts are everywhere, such as multi-threaded write shared variables, such as database multi-connection concurrent write data, such as PHP file functions such as multi-process write files

? ? ? ? In the case of concurrency, how does PHP write files? In fact, this problem is not just a problem facing PHP. Both threads and processes may encounter shared resource write conflicts when writing concurrently. During software development, write conflicts are everywhere, such as multi-threaded write shared variables, such as database multi-connection concurrent write data, such as multi-process write files. So what should we do? Currently, the common solution is to exclusively lock shared resources (write locks ).

? ? ? ? PHP provided a function flock in the third edition, as the name suggests, file lock operation function. The file lock mechanism depends on the host file system. that is to say, the host file system determines how to use the lock method. flock is only a shell function that calls the file system lock mechanism.

? ? ? ? There are two types of locks: read locks and write locks. Write locks are also called exclusive locks and exclusive locks. a thread or process occupies exclusive resources. other threads or processes cannot use resources, so that data writing is not disturbed and does not conflict with each other. read locks are also called Shared locks, multiple threads or processes are allowed to read at the same time, but no write operation is allowed.

Function prototype:

?

 flock ($handle, $operation, &$wouldblock = null);
 

?

Parameter description:

$ Handle // file pointer

$ Operation // lock type

$ Operation has several available values: LOCK_EX [write lock], LOCK_SH [read lock], and LOCK_UN [release lock]

? ? ? ? Let's look at an example.

?

 Function fileWrite ($ file) {$ fp = fopen ($ file, 'A'); flock ($ fp, LOCK_EX ); // write lock/* write data */fwrite ($ fp, "1"); fwrite ($ fp, "2"); fwrite ($ fp, "3 \ r \ n"); flock ($ fp, LOCK_UN); // release the lock fclose ($ fp);} fileWrite ('d:/txt.txt ');
 
? ? ? ? Run the concurrent test with Jmeter and execute 3000 requests, and you will find 3000 lines in the file, 123 (without considering the performance bottleneck of apache or nginx ).

?

? ? ? ? When using Jmeter for concurrent testing, open the file in Notepad, change something, and click Save. you will find the situation.



? ? ? ? This is because the file is locked and cannot be written by the notepad process.

? ? ? ? As mentioned above, the flock function lock mechanism is actually the file system lock mechanism, which encapsulates the lock mechanism of some types of file systems, and some file systems do not support flock, according to the PHP Manual, flock does not support older file systems such as FAT and NTF and network file systems. I have not tested it.

? ? ? ? The PHP Manual also says that flock is process-level and does not work when multithreading occurs. You may be confused. PHP has no thread concept. Put PHP on a server that supports multiple threads. Test once.

After analyzing this step, we will find that flock depends on the host and has requirements on the environment, which seriously damages the portability of code. Afraid !! Don't be afraid. In fact, the damage is not very deep. flock can still meet the needs of most environments.

?

? ? ? ? The portability of flock is a little short, so we started to study the alternative method. I also paid attention to it and found that the following code is circulating on the Internet:

?

 Function fileWrite ($ file, $ content) {$ lock = $ file. '. lock'; while (true) {if (file_exists ($ lock) {usleep (100);} else {touch ($ lock); // lock file_put_contents ($ file, $ content, FILE_APPEND); // write the file @ unlink ($ lock); // delete the lock break ;}} fileWrite ('d:/txt.txt ', 'I m a phper ');
 

? ? ? ? Do you think this code has a lock effect ?? Test !! In fact, no tests are required. at first glance, file_exists can be executed in parallel. when the two processes run to file_exists at the same time, it is determined that the so-called lock file does not exist and the results are not in conflict.

The lock actually blocks concurrent access queues in serialize mode, and then processes the access in sequence. the code above does not even have a queue. how can it be locked.

? ? At this time, you may ask multiple processes to execute flock at the same time. isn't it parallel? Yes, the execution of flock is parallel. this only means that the applied locks are parallel. the flock internally calls the host file system and will naturally queue the lock requests for processing.

?

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.