Usage of the flock function in C language in Linux

Source: Internet
Author: User
Tags flock

Header file # include <sys/file. h>

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

Function Description: Flock () performs various locking or unlocking actions on the file referred to by the parameter FD according to the method specified by the parameter operation. This function can only lock the entire file and cannot lock a certain area of the file.

Operation has the following four conditions:

Lock_sh creates a shared lock. Multiple processes can share and lock the same file at the same time.

Lock_ex creates a mutex lock. One file has only one mutex lock.

Lock_un unlocks the file.

Lock_nb cannot be used to establish a lock. This operation is not blocked and the process is returned immediately. Usually it is combined with lock_sh or lock_ex or (|.

Shared locks and mutex locks cannot be created for a single file. When DUP () or fork () is used, the file description does not inherit the lock.

If the return value is 0, a success is returned. If an error exists,-1 is returned. The error code is stored in errno.

 

Flock only needs to read and write the file after opening the file, and then flock it after use, lock the front and unlock the back. It is actually so simple, but some problems were found some time ago. The problem is described as follows:

One process opens the file, input an integer, and then the last write lock (lock_ex), then enter an integer to unlock (lock_un), and the other process opens the same file, write data directly to the file and find that the lock does not work and can be written normally (I am using a Super User ). Google found that flock does not provide a lock check, that is to say, before using flock, the user needs to check whether the lock has been applied, it indicates that the white point is to use flock to check whether the file is locked. If the flock is locked, it will block it (an attempt
Lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor) Unless lock_nb is used. A complete example code for testing is as follows:

// Lockfile. c

# Include <stdio. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <errno. h>

Int main ()
{
Int FD, I;
Char path [] = "/home/taoyong/test.txt ";
Extern int errno;
FD = open (path, o_wronly | o_creat );
If (FD! =-1)
{
Printf ("Open File % S./N", PATH );
Printf ("Please input a number to lock the file./N ");
Scanf ("% d", & I );
If (flock (FD, lock_ex) = 0)
{
Printf ("the file was locked./N ");
}
Else
{
Printf ("the file was not locked./N ");
}
Printf ("Please input a number to unlock the file./N ");
Scanf ("% d", & I );
If (flock (FD, lock_un) = 0)
{
Printf ("the file was unlocked./N ");
}
Else
{
Printf ("the file was not unlocked./N ");
}
Close (FD );

}
Else
{
Printf ("cannot open file % s/n", PATH );
Printf ("errno: % d/N", errno );
Printf ("errmsg: % s", strerror (errno ));
}
}

 

// Testprocess. c

# Include <stdio. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <errno. h>
# Include <sys/file. h>

Int main ()
{
Int FD, I;
Char path [] = "/home/taoyong/test.txt ";
Char s [] = "writing.../nwriting.../N ";
Extern int errno;
FD = open (path, o_wronly | o_creat | o_append );
If (FD! =-1)
{
Printf ("Open File % S./N", PATH );

If (flock (FD, lock_ex | lock_nb) = 0)
{
Printf ("the file was locked by the process./N ");
If (-1! = Write (FD, S, sizeof (s )))
{
Printf ("write % s to the file % s/n", S, PATH );
}
Else
{
Printf ("cannot write the file % s/n", PATH );
Printf ("errno: % d/N", errno );
Printf ("errmsg: % s/n", strerror (errno ));
}

}
Else
{
Printf ("the file was locked by other process. Can't write.../N ");
Printf ("errno: % d:", errno );
}

Close (FD );

}
Else
{
Printf ("cannot open file % s/n", PATH );
Printf ("errno: % d/N", errno );
Printf ("errmsg: % s", strerror (errno ));
}
}

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.