How to use php file locks? Php file lock tutorial

Source: Internet
Author: User
Tags flock
How to use php file locks? Php file lock tutorial

  1. $ File = "temp.txt ";
  2. $ Fp = fopen ($ file, 'w ');
  3. If (flock ($ fp, LOCK_EX )){
  4. Fwrite ($ fp, "abc \ n ");
  5. Sleep (10 );
  6. Fwrite ($ fp, "123 \ n ");
  7. Flock ($ fp, LOCK_UN );
  8. }
  9. Fclose ($ fp );

2) B. php

  1. $ File = "temp.txt ";
  2. $ Fp = fopen ($ file, 'r ');
  3. Echo fread ($ fp, 100 );
  4. Fclose ($ fp );

Run. run B immediately after php. php, you can see the output: abc and so on. run php after running B. php, you can see the output: abc123 obviously, when. when php writes a file, the data volume is too large, resulting in a long time. in this case, B. php Data reading is incomplete. the above methods are described in the php file locking and writing instance tutorial. if you do not understand them, refer to the examples in this article for more information.

Modify B. php:

  1. $ File = "temp.txt ";
  2. $ Fp = fopen ($ file, 'r ');
  3. If (flock ($ fp, LOCK_EX )){
  4. Echo fread ($ fp, 100 );
  5. Flock ($ fp, LOCK_UN );
  6. } Else {
  7. Echo "Lock file failed... \ n ";
  8. }
  9. Fclose ($ fp );

Run. run B immediately after php. php, you can find B. php will wait until. after php is run (10 seconds later), it shows that abc123 reads data completely, but it takes too long to wait for the write lock to be released.

Modify B. php:

  1. $ File = "temp.txt ";
  2. $ Fp = fopen ($ file, 'r ');
  3. If (flock ($ fp, LOCK_SH | LOCK_NB )){
  4. Echo fread ($ fp, 100 );
  5. Flock ($ fp, LOCK_UN );
  6. } Else {
  7. Echo "Lock file failed... \ n ";
  8. }
  9. Fclose ($ fp );

After running a. php, run B. php immediately. the output is Lock file failed... It proves that the lock file failure status can be returned, rather than waiting for a long time as it goes up.

Conclusion: We recommend that you select the relevant lock when caching the file. Otherwise, the read data may be incomplete or the data may be written repeatedly. File_get_contents does not seem to be able to select a lock and does not know what locks it uses by default. the reverse is the same as the output obtained by the unlock, which is incomplete data. I want to cache files, so I only need to know whether a write lock exists. If yes, I can check the database. Test Environment: Linux (Ubuntu 6), PHP 5.1.2, Apache 2

Conversion: There are two types of file locks: Shared locks and exclusive locks, that is, the LOCK_SH and LOCK_EX file locks are generally used as follows:

  1. $ Fp = fopen ("filename", "");
  2. Flock ($ fp, LOCK_SH) or die ("lock error ")
  3. $ Str = fread ($ fp, 1024 );
  4. Flock ($ fp, LOCK_UN );
  5. Fclose ($ fp );

Note that after fwrite, the file will be updated immediately, instead of waiting for fwrite and then fclose before the file will be updated. this can be checked by reading the file before fclose after fwrite.

But when will lock_ex be used?

1. read: if you do not want dirty data to appear, it is best to use the lock_sh shared lock. You can consider the following three cases: 1. if the shared lock is not applied during read, the write will be successful immediately if other programs want to write (whether the write is locked or not. If half of the data is read and written by other programs, the last half of the data read may be less than the first half (the first half is before the modification, the last half is modified. 2. if a shared lock is added during read (because it is only read, there is no need to use the exclusive lock), other programs start to write at this time, if the write program does not use a lock, the write program will directly modify the file, which will lead to the same problem as above. 3. the ideal situation is that the lock (lock_sh) is applied during read ), lock_ex is also implemented during write, so that the write program will wait for the Read program to complete the operation, without rashly performing the operation.

2. when writing: If multiple write programs operate on files without locking, part of the final data may be written by program, part of it is written by program B. If the lock is applied during write, other programs will read the lock at this time. What will it read? 1. if the Read program does not apply for a shared lock, it will read the data of dirty. For example, to write a program, write a, B, and c, and write a. At this time, read a and continue to write B. At this time, read AB and then write c, at this time, I read abc. 2. if the Read program has applied for a shared lock before, the Read program will read it only after the write program finishes writing abc and releases the lock.

The above is an introductory tutorial on php file locks. through comparative analysis, I will detail the usage of php file locks, hoping to help you.

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.