Linux file Lock flock

Source: Internet
Author: User
Tags flock mutex

Table header file #include <sys/file.h>

Defines the function int flock (int fd,int operation);

The function description flock () does various locking or unlocking actions on the file that the parameter FD refers to in the manner specified by the parameter operation. This function locks only the entire file and cannot lock a region of the file.

In the process of simultaneous operation of the same file in multiple processes, it is very easy to cause data confusion in the file, need to lock operation to ensure the integrity of the data, the file is described here in the lock, called "File lock"-flock.

Flock, the proposed sex lock, does not have the mandatory. A process uses flock to lock the file, another process can directly manipulate the file being locked, modify the data in the file, because flock is only used to detect whether the file is locked, for the file has been locked, the other process to write data, the kernel will not block the process of write operations, That is, the kernel processing strategy of the proposed lock.

The parameter operation has the following four conditions:

  Lock_sh Establish a shared lock. Multiple processes can share a lock on the same file at the same time.

  LOCK_EX establish a mutex lock. A file has only one mutex lock at a time.

  Lock_un Unlocks the file lock status.

  LOCK_NB Unable to establish a lock, this operation is not blocked and will return to the process immediately. Usually do or with Lock_sh or LOCK_EX (|) Combination.

A single file cannot establish both shared and mutex locks, and the file descriptor does not inherit this lock when using DUP () or fork ().

The return value returns 0 for success, and returns 1 if there is an error, and the error code is stored in errno.

When a process uses flock to try to lock a file, if the file is already locked by another process, the process is blocked until the lock is released, or when the flock is called, the LOCK_NB parameter is used, and when the file is attempted to be locked, the other service is locked and an error is returned. The errno error code is ewouldblock. This provides two modes of operation: blocking and non-blocking types. The service blocks waiting until the lock is released: The Flock (LOCKFD,LOCK_EX) service returns an error when the file is locked: ret = Flock (lockfd,lock_ex| LOCK_NB) at the same time ret =-1, errno = Ewouldblock flock The release of the lock is very special, you can call the Lock_un parameter to release the file lock, or by turning off FD mode to release the file lock (the first parameter of the flock is FD), This means that the flock will be automatically released as the process shuts down. Flock one of the usage scenarios is: Does the detection process already exist;
intCheckexit (Char*pfile) {     if(Pfile = =NULL)return-1; intLOCKFD =open (PFILE,O_RDWR); if(LOCKFD = =-1)          return-2; intIret = Flock (lockfd,lock_ex|lock_nb)if(Iret = =-1)          return-3; return 0;}

The following are examples of two processes:

file1.c

#include <stdio.h>#include<stdlib.h>#include<sys/file.h>#include<unistd.h>intMainvoid){    intfp = open ("file_lock.test", o_wronly); inti = -; if(fp = =-1)//Open File{printf ("File1 Open error!\n"); }    Else{printf ("File1 Open success!\n"); }    if(Flock (FP, lock_ex)! =0)//to lock the file{printf ("file1 lock by others\n"); }    Else{printf ("file1 no user by others\n"); }     while(1)//Enter the loop, lock time is 20 seconds, print Countdown{printf ("%d\n", i--); Sleep (1); if(i = =0)             Break; } close (FP); //exit after 20 seconds, close fileFlock (FP, lock_un);//file Unlock    return 0;}

File2.c

#include <stdio.h>#include<stdlib.h>#include<sys/file.h>#include<unistd.h>intMainvoid){    intfp = open ("file_lock.test", o_wronly); inti =0; if(fp = =-1)//Open File{printf ("File2 Open error!\n"); }Else{printf ("File2 Open success!\n"); }       if(Flock (FP, lock_ex)! =0)//to lock the file{printf ("file2 Lock by others\n"); }    Else{printf ("file2 no user by others\n"); }     while(1)//into the Loop{printf ("%d\n", i++); Sleep (1); } close (FP); //Close FileFlock (FP, lock_un);//Release file Lock    return 0; }

First run the file1.c, and then run file2.c (run file1.c after 20 seconds to run file2.c otherwise see phenomenon) phenomenon is: file1.c after the execution, began to Countdown. Running file2.c at this point will block at the lock. After the file1.c has been running for 20 seconds to close the file and release the file lock, FILE2.C will start running.

Linux file Lock flock

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.