Recommended Locks and mandatory locks

Source: Internet
Author: User
FCNTL has a powerful capability to copy an existing descriptor, get/Set file descriptor tags, get/Set file status tags, get/Set asynchronous I/O ownership, get/set record locks.

When multiple users work together to manipulate a file, Linux usually uses the method of locking the file to avoid competing status with shared resources.

There are two types of fcntl file locks: Recommended locks and mandatory locking recommended locks: every process that uses a locked file checks for locks and, of course, respects existing locks. The kernel and the system generally insist on not using the recommended locks, and they rely on the programmer to comply with this rule. (Linux defaults to a recommended lock) mandatory locks are performed by the kernel. When a file is locked for writing, the kernel blocks any read or write access to the file until the process that locks the file releases the lock, checking for the existence of the lock for each read or write access.

I/O operations with Fcntl file locks must be careful: how the process handles locks before starting any I/O operation, and how to complete all operations before unlocking the file, must be considered. If you open a file before you set up a lock, or if you close the file after reading the lock, another process may access the file in a fraction of a second between the lock/unlock operation and the open/close operation. When a process locks a file, whether or not it releases the lock that was added, as long as the file is closed, the kernel automatically releases the recommended locks added to the file (which is the biggest difference between the recommended and mandatory locks), so do not want to set up a proposed lock to achieve the goal of permanently not allowing other processes to access the file (mandatory locks are available) ^_^ ; Mandatory locks work on all processes. Example: Example 1, I have a few processes (not necessarily related) through the FCTNL mechanism to manipulate the file, this is called a consistent method. But, if at the same time, there is a rogue process, pipe it 3721, rushed up, open, write.
At this time, the process of fcntl to this way can not do nothing, so called inconsistent. The final state of the file is uncertain.
Because this lock cannot constrain other access modes, it is called a suggested row lock. Mandatory locks need to be supported by the kernel, and read, write, and open check for locks. For example 2, the so-called proposed lock is to assume that people will follow certain rules to do something. For example, people and cars to see the red light will stop, and see the green light will continue to walk, we can call red and green as the recommended lock. But it's just a rule, you don't prevent someone from running a red light. And the mandatory lock is that you want to run a red light.

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.