PHP file locks, shared locks, and exclusive locks

Source: Internet
Author: User
PHP file locks, shared locks, and exclusive locks. Note that there are two types of file locks: Shared locks and exclusive locks, that is, LOCK_SH and LOCK_EX)

File locks are generally used as follows:

$fp = fopen("filename", "a");   flock($fp, LOCK_SH) or die("lock error")   $str = fread($fp, 1024);   flock($fp, LOCK_UN);   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?

When reading:

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 situations:

1. if the shared lock is not applied during read, other programs will write the lock immediately (no matter 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), at this time, other programs start to write, this write program does not use the lock, then, writing a program will directly modify this file, which will lead to the same problem as above.

3. the ideal situation is that lock_sh is used during read and lock_ex is used during write. in this way, the write program will wait until the Read program is complete, there will be no hasty operations

When writing:

If multiple write programs operate on files without locking, some of the final data may be written by program a and some by program B.

If the lock is applied during the write process and other programs are used to read the lock, what will the program 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.

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.