Home > Backend Development > PHP Tutorial > How to use php file lock? PHP file lock introductory tutorial

How to use php file lock? PHP file lock introductory tutorial

WBOY
Release: 2016-07-25 08:53:04
Original
1078 people have browsed it
  1. $file = "temp.txt";
  2. $fp = fopen($file , 'w');
  3. if(flock($fp , LOCK_EX)){
  4. fwrite($fp , "abcn") ;
  5. sleep(10);
  6. fwrite($fp , "123n");
  7. flock($fp , LOCK_UN);
  8. }
  9. fclose($fp);
Copy code

2) b.php

  1. $file = "temp.txt";
  2. $fp = fopen($file , 'r');
  3. echo fread($fp , 100);
  4. fclose($fp);
Copy the code

After running a.php, run b.php immediately and you can see the output: abc After a.php has finished running, run b.php and you can see the output: abc 123 Obviously, when a.php writes the file, the data is too large and takes a long time. At this time, b.php reads the data incompletely. The above methods are introduced in the php file lock writing example tutorial. Friends who don’t understand can refer to the examples in the article to learn more.

Modify b.php to:

  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);
Copy the code

After running a.php, Run b.php immediately, you can find that b.php will wait until a.php is completed (that is, after 10 seconds) before displaying: abc 123 The read data is complete, but the time is too long, and he has to wait for the write lock to be released.

Modify b.php to:

  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);
Copy code

Run a.php After that, run b.php immediately and you can see the output: Lock file failed… It is proved that the lock file failure status can be returned instead of waiting for a long time as above.

Conclusion: It is recommended to select relevant locks when caching files, otherwise the read data may be incomplete or the data may be written repeatedly. File_get_contents seems to be unable to select a lock. I don’t know what lock it uses by default. Anyway, the output obtained by not locking is the same as incomplete data. I want to do file caching, so I only need to know whether there is a write lock, and if so, just check the database. Test environment: Linux (Ubuntu 6), PHP 5.1.2, Apache 2

Repost: There are two types of file locks: shared locks and exclusive locks, namely read locks (LOCK_SH) and write locks (LOCK_EX) File locks are generally used like this:

  1. $fp = fopen("filename", "a");
  2. flock($fp, LOCK_SH) or die("lock error")
  3. $str = fread($fp, 1024);
  4. flock ($fp, LOCK_UN);
  5. fclose($fp);
Copy code

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

But when to use lock_ex and when to use lock_sh?

One, when reading: If you do not want dirty data to appear, it is best to use lock_sh shared lock. The following three situations can be considered: 1. If no shared lock is added when reading, then if other programs want to write (regardless of whether the write is locked or not), the write will be successful immediately. If exactly half of it is read and then written by another program, then the second half of the read may not match the first half (the first half is before modification, and the second half is after modification) 2. If a shared lock is added when reading (because it is just reading, there is no need to use an exclusive lock), at this time, other programs start to write, and the writing program does not use the lock, then the writing program will directly modify the file, which will also cause Same problem as before 3. The most ideal situation is to lock (lock_sh) when reading and lock (lock_ex) when writing. In this way, the writing program will wait for the reading program to complete before operating, and there will be no rash operations.

Second, when writing: If multiple writing programs operate on the file at the same time without locking, then part of the final data may be written by program a and part by program b. If it is locked when writing, and other programs come to read it at this time, what will it read? 1. If the reader does not apply for a shared lock, then it will read dirty data. For example, when writing a program, you need to write three parts a, b, and c. After writing a, what you read at this time is a. Continue writing b. At this time, you read ab. Then write c. At this time, what you read is abc. . 2. If the reading program has applied for a shared lock before, the reading program will wait for the writing program to finish writing abc and release the lock before reading.

The above is an introductory tutorial on PHP file lock. It details the usage of PHP file lock through comparative analysis. I hope it will be helpful to everyone.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template