File lock operation for Linux applications

Source: Internet
Author: User
In Linux, the file lock operation is mainly completed through the following two APIs. # Include & lt; sys/file. h & gt; intflock (intfd, interval peration); or # include & lt; unistd. h & gt; # include & lt; fcntl. h & gt; intfcntl (intfd, intcmd); int

In Linux, the file lock operation is mainly completed through the following two APIs.
 
 
# Include
Int flock (int fd, int operation );
 
Or
 
 
# Include
# Include
Int fcntl (int fd, int cmd );
Int fcntl (int fd, int cmd, long arg );
Int fcntl (int fd, int cmd, struct flock * lock );
Note: The former is mainly used to lock the entire file, and the latter can lock part of the file.
 
 
Pay attention to the following points when Programming Linux applications:
1) whether the file lock targets the entire file or part of the file.
2) if the process-level file handle is closed, the file lock will be released.
3) to modify the file content, you must note the glibc buffer mechanism to synchronize data in a timely manner.
4) flock locks inode and fcntl lock file descriptor. Therefore, flock does not support NFS. Note the compatibility.
 
The demo code of the process-level and line-level file lock is provided for reference.
Process-level file lock demo:
 
 
# Include
# Include
# Include
# Include
# Include
 
# Define TEST_FOPEN
 
Int main (int argc, char * argv [])
{
/* L_type l_whence l_start l_len l_pid */
Struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0 };
Int fd;
 
# Ifdef TEST_FOPEN
FILE * file = NULL;
# Endif/* TEST_FOPEN */
 
Fl. l_pid = getpid ();
 
If (argc> 1)
Fl. l_type = F_RDLCK;
 
While (1)
{
# Ifdef TEST_FOPEN
If (file = fopen ("lockdemo. c", "rw +") = NULL ){
Perror ("fopen ");
Exit (1 );
}
# Else
If (fd = open ("lockdemo. c", O_RDWR) =-1 ){
Perror ("open ");
Exit (1 );
}
# Endif/* TEST_FOPEN */
Printf ("Press To try to get lock :");
Getchar ();
Printf ("Trying to get lock ...");
 
# Ifdef TEST_FOPEN
Fd = fileno (file );
# Endif/* TEST_FOPEN */
 
Fl. l_type = F_WRLCK;/* set to lock same region */
If (fcntl (fd, F_SETLKW, & fl) =-1 ){
Perror ("fcntl ");
Exit (1 );
}
 
Printf ("got lock \ n ");
Printf ("Press To release lock :");
Getchar ();
 
Fl. l_type = F_UNLCK;/* set to unlock same region */
 
If (fcntl (fd, F_SETLK, & fl) =-1 ){
Perror ("fcntl ");
Exit (1 );
}
 
Printf ("Unlocked. \ n ");
# Ifdef TEST_FOPEN
Fclose (file );
# Else
Close (fd );
# Endif/* TEST_FOPEN */
}
 
Return 0;
}



Running result:

 


 

Thread-level file lock demo:
# Include
# Include
# Include
# Include
# Include
# Include
 
# Define TEST_FOPEN
# Define TEST_FLOCK
 
Void * thread_flock (void * ptr)
{
/* L_type l_whence l_start l_len l_pid */
Struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0 };
Int fd;
 
Int ith = * (int *) ptr );
 
# Ifdef TEST_FOPEN
FILE * file = NULL;
# Endif/* TEST_FOPEN */
 
Fl. l_pid = getpid ();
 
While (1)
{
# Ifdef TEST_FOPEN
If (file = fopen ("lockdemo. c", "rw +") = NULL ){
Perror ("fopen ");
Exit (1 );
}
# Else
If (fd = open ("lockdemo. c", O_RDWR) =-1 ){
Perror ("open ");
Exit (1 );
}
# Endif/* TEST_FOPEN */
 
# Ifdef TEST_FOPEN
Fd = fileno (file );
# Endif/* TEST_FOPEN */
 
# Ifdef TEST_FLOCK
Flock (fd, LOCK_EX );
# Else
Fl. l_type = F_WRLCK;/* set to lock same region */
If (fcntl (fd, F_SETLKW, & fl) =-1 ){
Perror ("fcntl ");
Exit (1 );
}
# Endif/* TEST_FLOCK */
 
Printf ("[% d] % d --> got lock \ n", ith, fd );
Sleep (ith );
 
# Ifdef TEST_FLOCK
Flock (fd, LOCK_UN );
# Else
Fl. l_type = F_UNLCK;/* set to unlock same region */
 
If (fcntl (fd, F_SETLKW, & fl) =-1 ){
Perror ("fcntl ");
Exit (1 );
}
# Endif/* TEST_FLOCK */
 
Printf ("[% d] % d --> Unlocked. \ n", ith, fd );
# Ifdef TEST_FOPEN
Fclose (file );
# Else
Close (fd );
# Endif/* TEST_FOPEN */
 
Sleep (2 );
}
}
 
Int main (int argc, char * argv [])
{
Int time1, time2;
 
Pthread_t pid1, pid2;
 
Time1 = 1;
Pthread_create (& pid1, NULL, & thread_flock, & time1 );
 
Time2 = 3;
Pthread_create (& pid2, NULL, & thread_flock, & time2 );
 
While (1)
Sleep (10 );
 
Return 0;
}
Running result:


 


 

 

References:

[1] Linux man pages

[2] fcntl () for thread or process synchronization?

[3] How to lock files using fcntl () and to work between threads of the same process

[4] mutex locks created by fcntl + pthread_rwlock that support multi-process and multi-thread mixing

[5] flock does not support NFS

[6] Re: Linux, fcntl vs flock and pthreads


From lida2003's column
 

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.