File lock in Linux

Source: Internet
Author: User
File lock in Linux-general Linux technology-Linux programming and kernel information. For more information, see the following section. There are three different file locks, all of which are "consulting". That is to say, they rely on the cooperation between programs. Therefore, it is very important to ensure the consistency of all program blocking policies in a project, be especially careful when your program needs to share files with third-party software.

Some programs use file lock files such as FIlENAME. lock, and then simply test whether such files exist. This method is obviously not good, because when the process that generates the file is killed, the lock file still exists, and the file may be permanently locked. UUCP stores the PID of the process that generates the file into the file, but it is not safe to do so because the PID is recycled.

Here are three file lock functions:

Flock ();
Lockf ();
Fcntl ();

Flock () is derived from BSD, but it can be found in most UNIX systems at present. flock () is simple and effective on a single host, but it cannot work on NFS. Perl also has a somewhat confusing flock () function, but it is implemented within perl.

Fcntl () is the only POSIX-compliant file lock implementation, so it is also the only portable. It is also the most powerful file lock-and the most difficult to use. On the NFS file system, fcntl () requests are submitted to rpc. the lockd daemon, which then communicates with the host lockd. Unlike flock (), fcntl () can implement blocking on the record layer.

Lockf () is just a simplified fcntl () file lock interface.

No matter which file lock you use, remember to use sync to update all your file inputs/outputs before the lock takes effect.

Lock (fd );
Write_to (some_function_of (fd ));
Flush_output_to (fd);/* Be sure to fl the output before removing the lock */
Unlock (fd );
Do_something_else;/* Another process may update it */
Lock (fd );
Seek (fd, somewhere);/* because the original file pointer is no longer secure */
Do_something_with (fd );...

Some useful fcntl () blocking methods (for simplicity, skip error handling ):

# Include ;
# Include ;

Read_lock (int fd)/* a shared file lock on the entire file */
{
Fcntl (fd, F_SETLKW, file_lock (F_RDLCK, SEEK_SET ));
}

Write_lock (int fd)/* Exclusive file lock on the entire file */
{
Fcntl (fd, F_SETLKW, file_lock (F_WRLCK, SEEK_SET ));
}

Append_lock (int fd)/* a lock that blocks the end of a file,
Other processes can access the existing content */
{
Fcntl (fd, F_SETLKW, file_lock (F_WRLCK, SEEK_END ));
}

The previously used file_lock function is as follows:

Struct flock * file_lock (short type, short whence)
{
Static struct flock ret;
Ret. l_type = type;
Ret. l_start = 0;
Ret. l_whence = whence;
Ret. l_len = 0;
Ret. l_pid = getpid ();
Return & ret;
}

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.