Besides, PHP file lock, shared lock and exclusive lock attention point

Source: Internet
Author: User
Tags flock
There are two types of file locks: shared and exclusive, that is, read lock (LOCK_SH) and write lock (LOCK_EX)

File locks are typically used in this way:

$fp = fopen ("filename", "a");   Flock ($FP, lock_sh) or Die ("LOCK error")   $str = Fread ($fp, 1024x768);   Flock ($FP, lock_un);   Fclose ($FP);

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

But when to use LOCK_EX when to use Lock_sh?

When reading:

If you do not want dirty data to appear, it is best to use lock_sh shared locks. Here are three things to consider:

1. If the reader does not have a shared lock, then other programs to write (whether the write is locking or unlocked) will immediately write success. If you just read half of it and then write it to another program, then half of the reading is probably half right (the first half is a modified one and the last half is a modified one).

2. If you add a shared lock when reading (because it is just read, there is no need to use an exclusive lock), this time, the other program began to write, the program does not use the lock, then write the program will directly modify the file, will also lead to the same problem

3. Ideally, when reading locking (LOCK_SH), when writing also to lock (LOCK_EX), so that the program will wait until the completion of the program before the operation, without any hasty operation of the situation

When writing:

If multiple write programs do not lock the file at the same time, then the final data may be part of a program written, part of the B program is written

If it was locked when it was written, and there were other programs to read, what would he read?

1. If the reader does not request a shared lock, he will read the dirty data. For example, write a program to write a,b,c three parts, write a, this time read a, continue to write B, at this time read the AB, and then write C, this time read ABC.

2. If the reader has previously applied for a shared lock, the read program will wait until the program writes out the ABC and releases the lock before reading.

  • Related Article

    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.