One, what is the file lock
To lock the word, we must not unfamiliar, because we live in a large number of locks, they play its part in every way, and now the world's lock function can be summed up in a sentence, is to prevent some people to do something, for example, the door lock is to prevent people except the owner into the house, you do not enter the house , you can't use the things inside the house.
And because programs often need to share data, and this is usually done through a file, what happens when a process is writing a file and another program B needs to read the same file and read the data as the data it needs to run its own program in? Process B may read the garbled data because it does not know that another process A is overwriting the data in this file.
In order to solve a similar problem, there is a file lock, in simple terms, this is a safe way to update a file, and when a program is writing to a file, it goes into a temporary state where another program will automatically stop and wait for the state to end if it tries to read the file. Linux provides a number of features for file locking, the simplest of which is to create a lock file as an atomic operation.
In the case of the previous example, a file lock is when a file is written, blocking other processes that need to be written or read in order to manipulate the file.
Second, create a lock file
Creating a lock file is very simple, we can use the open system call to create a lock file, when calling open Oflags parameters to increase the parameters o_creat and O_EXCL flags, such as File_desc = Open ("/tmp/lck.test", O_ rdwr| O_creat| O_EXCL, 0444); You can create a lock file/tmp/lck.test. O_creat| O_EXCL, you can make sure that the caller can create a file that prevents two programs from creating the same file at the same time, and if the file (/tmp/lck.test) already exists, the open call fails and returns-1.
If a program is executing on it, you just need to monopolize a resource for a short time, which is often called a critical section, and we need to create a lock file using open system calls before entering the critical section, and then delete the lock file with unlink system calls when exiting the critical section.
Note: Lock files only act as an indicator, and programs need to be used in collaboration with each other, which means that locking files is only a recommended lock, not a mandatory lock, and does not actually prevent you from reading and writing data from files.
Take a look at the following example: The source file file name is filelock1.c, the code is as follows:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
int main ()
{
const char *lock_file = "/tmp/lck.test1";
int n_fd =-1;
int n_tries = ten;
while (n_tries--)
{
//Create lock file
n_fd = open (Lock_file, o_rdwr| O_creat| O_EXCL, 0444);
if (n_fd = = 1)
{
//Create failed
printf ("%d-lock already present\n", getpid ());
Sleep (2);
}
else
{
//Create successful
printf ("%d-i have exclusive access\n", Getpid ());
Sleep (1);
Close (N_FD);
Delete lock file, release lock
unlink (lock_file);
Sleep (2);
}
}
return 0;
}
Run two instances of the same program at the same time, and the results are:
From the results of the operation can be seen two of the program cross to lock the file, but the real operation is, each call to the Open function to check the/tmp/ Lck.test1 If the file exists, if an open call fails, the process has locked the file, and if the file does not exist, create the file and display the license information. But this approach has some drawbacks, we can see that the file/tmp/lck.test1 was created many times, but also by the unlink deleted many times, that is, we can not use the data has been in advance of the file as such a lock file, because if the file already exists, the open call will always fail.
The feeling is that it's more like a coordinated arrangement of processes, more like binary semaphores, where files exist as 0, not 1, not real file locks.