File lock in Linux

Source: Internet
Author: User
Tags flock
How to lock files

There are three different file locks, all of which are "consulting", that is, they rely onProgramBetween
Cooperation, so the consistency of all program blocking policies in a project is very important when your program needs
Be especially careful when sharing 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 currently.
Flock () on the machine is simple and effective, but it cannot work on NFS. Perl is also a bit confusing.
The flock () function is implemented in Perl.

Fcntl () is the only POSIX-compliant file lock implementation, so it is also the only portable. It is also the same
Is the most powerful file lock-it is also the most difficult to use. On the NFS file system, fcntl () requests are delivered
To the rpc. lockd daemon, and then it is responsible for dialog with the host lockd, and flock ()
Different, 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 files before the lock takes effect.
Input/output.

[Code] 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); [/Code]...

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

[Code] # include <fcntl. h>;
# Include <unistd. h>;

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 ));
} [/Code]
The previously used file_lock function is as follows:

[Code] 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;
} [/Code]

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.