Previous section: http://blog.csdn.net/mybelief321/article/details/8989755 5 basic function Functions open (), read (), write (), Lseek (), and Close () The implementation of the file open, read/write operations, this section will discuss how the file has been shared, that is, when multiple users to use, manipulate a file. At this point, Linux is often used to lock files to avoid the competing state of shared resources.
File locks include recommended locks and mandatory locks. The proposed lock requires that each locked file process check for a lock and respect the existing lock. In general, the kernel and the system do not use a recommended lock. A mandatory lock is a lock executed by the kernel, which prevents any other file from reading and writing when a file is locked for writing. The use of mandatory locks has a great impact on performance, each read and write must check whether there is a lock.
In Linux, a file-locked function has LOCKF () and Fcntl (), where LOCKF () is used to impose a recommended lock on the file, while Fcntl () can impose a mandatory lock as well as a recommended lock. At the same time, fcntl () can also lock a record of a file, that is, record the lock.
Record locks can be divided into read locks and write locks, where read locks are also known as shared locks, which enable multiple processes to establish read locks in the same part of the file. A write lock is also known as an exclusion lock, and only one process can establish a write lock in a certain part at any time. Of course, both read and write locks cannot be established in the same part of the file.
The Fcntl () function is rich in functionality that allows you to perform various operations on an open file descriptor, including not only managing file locks, but also getting a set of file descriptors and file descriptor flags, and copying file descriptors! This time I'm going to take a look at the Fcntl () function to create a file lock, about its other usage ... Let's learn this first.
Fcntl () function format
The lock in table 1 is a flock structure with the following structure:
The off_t in the figure above is the data type long int; pid_t is the data type int, and there's an explanation: dot this explanation
So the value of each variable in lock of this structure is as follows table 2