Differences between fcntl (), lockf, and flock in linux _ PHP Tutorial

Source: Internet
Author: User
Tags flock
Differences between fcntl (), lockf, and flock in linux. What are the differences between fcntl (), lockf, and flock in linux? what are the differences between fcntl (), lockf, and flock? the functions of lvyilong316 are used to lock files? First, the differences between fcntl (), lockf, and flock in floc linux

Differences between fcntl (), lockf, and flock
-- Lvyilong316

These three functions are used to lock files. What are the differences between them? First, flock and fcntl are system calls, while lockf is a library function. Lockf is actually the encapsulation of fcntl, so the underlying implementation of lockf and fcntl is the same, and the effect of file locking is also the same. Fcntl and lockf are put together in most cases when different points are analyzed later. Next we will first look at the use of each function, from the use of the method and effect to see the difference between each function.

1. flock

L function prototype

# Include

Intflock (intfd, interval peration); // Applyorremoveanadvisorylockontheopenfilespecifiedbyfd, just the unlock lock

Fd is the file descriptor returned by the system to call open. operation options include:

LOCK_SH: shared Lock

LOCK_EX: Exclusive lock or exclusive lock

LOCK_UN: Unlock.

LOCK_NB: non-blocking (used together with the above three operations)

For the flock function, you must first know that the flock function can only lock the entire file, but not a part of the file. this is the first important difference between fcntl and lockf, the latter can lock a certain area of the file. Second, flock can only generate an advisory lock. We know that there are mandatorylock and advisorylock in linux ). The so-called forced lock is easy to understand. it is the lock on the door of your home. The most terrible thing is that there is only one key and only one process can operate. The so-called advisory lock is essentially a protocol. Before you access a file, check the lock first. at this time, the lock will only work. if you are not so kind, you should read and write the locks no matter if you are not kind, then the suggested lock has no effect. The process that follows the protocol and checks the lock before reading and writing is called a cooperative process. Again, the difference between flock and fcntl/lockf lies mainly in fork and dup.

(1) the locks created by flock are associated with the structfile, rather than fd. This means that after copying the file fd (through fork or dup), you can use both fd to operate the lock (for example, locking through one fd and releasing the lock through another fd ), that is to say, the child process inherits the lock of the parent process. However, when one fd is closed during the lock process, the lock will not be released (because the file structure is not released). the lock will be released only when all copies of fd are closed. Test program input program 1.

L Program 1


 
 
  1. #include
  2. #include
  3. #include
  4. #include
  5. int main (int argc, char ** argv)
  6. {
  7. int ret;
  8. int fd1 = open("./tmp.txt",O_RDWR);
  9. int fd2 = dup(fd1);
  10. printf("fd1: %d, fd2: %d\n", fd1, fd2);
  11. ret = flock(fd1,LOCK_EX);
  12. printf("get lock1, ret: %d\n", ret);
  13. ret = flock(fd2,LOCK_EX);
  14. printf("get lock2, ret: %d\n", ret);
  15. return 0;
  16. }

The running result locks fd1 and does not affect the lock of the program through fd2. For parent and child processes, see program 2.

L procedure 2

 
 
  1. #include
  2. #include
  3. #include
  4. #include
  5. int main (int argc, char ** argv)
  6. {
  7. int ret;
  8. int pid;
  9. int fd = open("./tmp.txt",O_RDWR);
  10. if ((pid = fork()) == 0){
  11. ret = flock(fd,LOCK_EX);
  12. printf("chile get lock, fd: %d, ret: %d\n",fd, ret);
  13. sleep(10);
  14. printf("chile exit\n");
  15. exit(0);
  16. }
  17. ret = flock(fd,LOCK_EX);
  18. printf("parent get lock, fd: %d, ret: %d\n", fd, ret);
  19. printf("parent exit\n");
  20. return 0;
  21. }

In the running result, the sub-process holds the lock and does not affect the parent process to obtain the lock through the same fd, and vice versa.

(2) use open to open the same file twice. the Two fd files are independent (because the underlying layer corresponds to two file objects). one of them is locked and cannot be unlocked through the other, in addition, the lock cannot be performed before the previous unlock. The test procedure is as follows:

L program 3

 
 
  1. #include
  2. #include
  3. #include
  4. #include
  5. int main (int argc, char ** argv)
  6. {
  7. int ret;
  8. int fd1 = open("./tmp.txt",O_RDWR);
  9. int fd2 = open("./tmp.txt",O_RDWR);
  10. printf("fd1: %d, fd2: %d\n", fd1, fd2);
  11. ret = flock(fd1,LOCK_EX);
  12. printf("get lock1, ret: %d\n", ret);
  13. ret = flock(fd2,LOCK_EX);
  14. printf("get lock2, ret: %d\n", ret);
  15. return 0;
  16. }

As a result, the lock cannot be obtained through fd2.

(3) after exec is used, the file lock status remains unchanged.

(4) flock cannot be used on the NFS file system. if you want to use a file lock on NFS, use fcntl.

(5) flock locks can be recursive, that is, two fd generated by dup or fork can be locked without deadlock.

2. lockf and fcntl

L function prototype

# Include

Intlockf (intfd, intcmd, off_tlen );

Fd is the open file descriptor returned through open.

The cmd value is:

F_LOCK: locks the file. if the file is locked, the lock is blocked until it is released.

F_TLOCK: same as F_LOCK, but if the file has been locked, it will not be blocked, and an error will be returned.

F_ULOCK: Unlock.

F_TEST: test whether the file is locked. if the file is not locked, 0 is returned; otherwise,-1 is returned.

Len: the length to be locked from the beginning of the current position of the file.

Through the function parameter function, we can see that lockf only supports exclusive locks and does not support shared locks.

# Include

# Include

Intfcntl (intfd, intcmd,.../* arg */);

Structflock {

...

Shortl_type;/* Typeoflock: F_RDLCK, F_WRLCK, F_UNLCK */

Shortl_whence;/* Howtointerpretl_start: SEEK_SET, SEEK_CUR, SEEK_END */

Off_tl_start;/* Startingoffsetforlock */

Off_tl_len;/* Numberofbytestolock */

Pid_tl_pid;/* PIDofprocessblockingourlock (F_GETLKonly )*/

...

};

There are three types of cmd related to file record locking:

F_SETLK: apply for a lock (read the lock F_RDLCK, write the lock F_WRLCK) or release the lock (F_UNLCK). However, if the kernel cannot grant the lock to the current process (the lock is occupied by other processes first ), an error is returned.

F_SETLKW: almost the same as F_SETLK. The only difference is that it is a dead-eye master. if you cannot apply for it, you will be stupid.

F_GETLK: this interface obtains the lock information. this interface modifies the structflock we passed in.

Through the function parameter function, we can see that fcntl is the most powerful function. it supports both shared locks and exclusive locks, that is, it can lock the entire file and only lock a part of the file.

The following describes the features of fcntl/lockf:

(1) locks can be recursive. if a process has a lock on a file range, and later the process attempts to add a lock in the same range, the new lock will replace the old lock.

(2) the read lock (shared lock) file must be read and opened, and the write lock (exclusive lock) file must be read and opened.

(3) a process cannot use the F_GETLK command to test whether a part of the file itself holds a lock. The F_GETLK command defines whether the returned information indicates whether the existing lock is blocked by the calling process to set its own lock. Because the F_SETLK and F_SETLKW commands always replace the existing locks of the process, the calling process will never block the locks held by itself. Therefore, the F_GETLK command will never report the locks held by the calling process.

(4) when the process is terminated, all the file locks established by him will be released, and the team doctor flock will also be the same.

(5) when a descriptor is disabled at any time, any lock on the file that the process can reference through this descriptor is released (these locks are set by the process ), this is different from flock. For example:

Fd1 = open (pathname ,...);

Lockf (fd1, F_LOCK, 0 );

Fd2 = dup (fd1 );

Close (fd2 );

After close (fd2), the lock set on fd1 will be released. if dup is changed to open the same file on another descriptor, the effect will be the same.

Fd1 = open (pathname ,...);

Lockf (fd1, F_LOCK, 0 );

Fd2 = open (pathname ,...);

Close (fd2 );

(6) the child process generated by fork does not inherit the lock set by the parent process, which is different from flock.

(7) after executing exec, the new program can inherit the lock of the original program, which is the same as that of flock. (If you set close-on-exec for fd, the fd will be closed before exec, and the lock of the corresponding file will be released ).

(8) support for forced lock: enable the set group ID (S_ISGID) for a specific file, and disable its group execution bit (S_IXGRP), then enable the mandatory lock mechanism for the file. In Linux, if you want to use a mandatory lock, you must use _ omand to enable this mechanism when mounting the file system.

3. relationship between the two locks

So what is the relationship between flock and lockf/fcntl locks? Answers do not affect each other. The test procedure is as follows:

 
 
  1. #include
  2. #include
  3. #include
  4. #include
  5. int main(int argc, char **argv)
  6. {
  7. int fd, ret;
  8. int pid;
  9. fd = open("./tmp.txt", O_RDWR);
  10. ret = flock(fd, LOCK_EX);
  11. printf("flock return ret : %d\n", ret);
  12. ret = lockf(fd, F_LOCK, 0);
  13. printf("lockf return ret: %d\n", ret);
  14. sleep(100);
  15. return 0;
  16. }

The test results are as follows:

$./A. out

Flockreturnret: 0

Lockfreturnret: 0

It can be seen that the locking of flock does not affect the locking of lockf. You can view the status of the lock obtained by the process through/proc/locks.

$ Psaux | grepa. out | grep-vgrep

123721388490.0011611904440pts/5 S + 01: 090: 00./a. out

$ Sudocat/proc/locks | grep18849

1: POSIXADVISORYWRITE1884908: 02: 8526740EOF

2: FLOCKADVISORYWRITE1884908: 02: 8526740EOF

We can see the lock information under/proc/locks: I will describe the meaning of the lock respectively:

1) POSIXFLOCK: the exact type of lock. Flock system calls generate FLOCK. fcntl calls F_SETLK, F_SETLKW, or lockf to generate the POSIX type. it can be seen that the lock types produced by the two calls are different;

2) ADVISORY indicates that it is an ADVISORY lock;

3) WRITE, as its name implies, is a WRITE lock and a read lock;

4) 18849 is the ID of the process holding the lock. Of course, for a flock type lock, the process may be exited.

5) 08: 02: 852674 indicates that the primary device of the device corresponding to the disk file is good, the secondary device number, and the inodenumber corresponding to the file.

6) 0 indicates the actual location.

7) EOF indicates the end position. These two fields are useful for the fcntl type. For flock, they are always 0 and EOF.

What are the differences between lift (), lockf, and flock? the difference between fcntl (), lockf, and flock -- lvyilong316 functions are used to lock files. What are the differences between them? First floc...

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.