There are two types of fcntl File locks:
Create locksAnd
Mandatory lock
The locks are defined as follows: every process that uses a lock file must check whether a lock exists. Of course, you must respect the existing locks. In general, the kernel and system insist on not using the locks. They rely on programmers to comply with this rule.
The forced lock is executed by the kernel. When a file is locked for write operations, the kernel will block any read or write access to the file before the process that locks the file releases the lock, check whether the lock exists for each read or write access.
By default, fcntl locks are built, and the mandatory locks are non-POSIX standard. If you want to use a mandatory lock and enable the system to use a mandatory lock, you need to re-mount the file system. Mount uses the-0 mand parameter to open the mandatory lock, you can also disable the group execution permission for the locked file and enable the set-gid permission bit for the file.
The unlock lock is only useful between cooperating processes. The understanding of cooperating process is the most important. It refers to the process that will affect other processes or be affected by other processes, here are two examples: (1) We can run the same command in both windows to operate on the same file. Then the two processes are cooperating processes; (2) cat file | sort, the processes generated by CAT and sort use the cooperating processes of pipe.
You must be careful when using the fcntl file lock for I/O operations: how the process processes process the lock before starting any I/O operations, how to complete all operations before unlocking the file, it 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 several minutes between the lock/unlock operation and the open/close operation. After a process locks a file, whether or not it releases the lock, as long as the file is closed, the kernel Automatically releases the lock applied to files (this is also the biggest difference between the lock and the mandatory lock ), therefore, do not set an authorization lock to prevent other processes from accessing files permanently (only mandatory locks are supported) ^_^; Mandatory locks work for all processes.
Fcntl uses the f_setlk/f_setlkw, f_unlck, and f_getlk parameters to request, release, and test the record locks. The record locks are the locks for a part of the file rather than the entire file, this refined control allows the process to better collaborate to share file resources. Fcntl can be used to read and write locks. Read locks are also called shared locks because multiple cooperating processes can create read locks in the same part of the file; write lock is called an exclusive lock because only one cooperating process can create a write lock on a part of the file at any time. If cooperating processes is used to operate files, you can add read lock to the files at the same time. Before a cooperating process is added with write lock, other cooperating processes must be released and added to the read lock and wrtie lock of the file. That is to say, only one write lock exists in the file. The read lock and wrtie lock cannot coexist.
Original article addressHttp://www.mcublog.com/blog/user1/6865/archives/2007/21406.html