Mandatory lock advisory lock

  
 

fcntl has a powerful feature that can copy an existing descriptor, get/set file descriptor tags, get/set file status flags, get/set asynchronous I/O ownership, get/set record locks.

When multiple users use together and operate a file, Linux usually uses a method to lock files to avoid competing resources.

fcntl file locks are of two types: advisory locks and mandatory locks • advisory locks are defined as follows: each process that uses a lock file checks to see if there is a latch, of course Respect the existing locks. The kernel and the system generally insist on not using advisory locks, and they rely on the programmer to comply with this rule. (Linux defaults to use advisory locks) • Mandatory locks are enforced by the kernel. When a file is locked for a write operation, the kernel blocks any read or write access to the file before the process that locks the file releases the lock. Each read or write access checks for the existence of the lock.

The use of fcntl file locks for I/O operations must be careful: how the process handles the lock before starting any I/O operations, and how to do everything before unlocking the file must be considered. If the file is opened before the lock is set, or the file is closed after the lock is read, another process may access the file within 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 added lock, as long as the file is closed, the kernel automatically releases the suggested lock attached to the file (this is also the biggest difference between the recommended lock and the mandatory lock), so Don't want to set a suggestion lock to achieve the goal of permanently not allowing other processes to access the file (mandatory locks are available) ^_^; mandatory locks work for all processes.

The functions that implement locking in Linux are lock() and fcntl(). • lock() is used to apply advisory locks to files • fcntl() is used to apply advisory and mandatory locks to files. At the same time, it is also possible to lock a certain record of the file, that is, record the lock.

Example: • Example 1, I have several processes (not necessarily related) that use the fctnl mechanism to manipulate files. This is called a consistent method. See [2] However, if there is another rogue process at the same time, it is managed 3721, rushed up, open, write. At this time, the process fcntl can't do anything about this way, so it is called inconsistency. The final state of the file is not fixed. Because this lock does not restrict other access methods, it is called a recommended row lock. Mandatory locks require kernel support, and locks are checked for read, write, and open. • Example 2, the so-called suggestive lock is to assume that people will follow certain rules to do one thing. For example, when people and cars see red light, they will stop, and when they see green light, they will continue to go. We can call red and green as recommended locks. But this is just a rule. You don't prevent some people from smashing red lights. The mandatory lock is that you want to smash the red light. See [3]

How do I use mandatory locks on Linux systems?

If you want linux to support mandatory locks, you need to add -o mand when you mount (APUE Chinese version is wrong, it is written as _omand), and you need to set the files to be locked. Related to (turn on set-group-ID and turn off group-execute) see [4].

Copyright © Windows knowledge All Rights Reserved