Previous operations for files were typically done in one process, and the same file was recently required to be manipulated in two processes. So I thought of the file lock.
Under Linux, you can use the flock () function to lock and unlock files. A brief introduction to the following flock () function:
Table header File #include
Defines the function int flock (int fd,int operation);
The function description flock () does various locking or unlocking actions on the file that the parameter FD refers to in the manner specified by the parameter operation. This function locks only the entire file and cannot lock a region of the file.
The parameter operation has the following four conditions:
Lock_sh establish a shared lock. Multiple processes can share a lock on the same file at the same time.
LOCK_EX establish a mutex lock. A file has only one mutex lock at a time.
Lock_un unlocks the file lock status.
LOCK_NB unable to establish a lock, this operation is not blocked and will return to the process immediately. Usually do or with Lock_sh or LOCK_EX (|) Combination.
A single file cannot establish both shared and mutex locks, and the file descriptor does not inherit this lock when using DUP () or fork ().
The return value returns 0 for success, and returns 1 if there is an error, and the error code is stored in errno.
For better portability, the opening and closing of files I chose the combination of fopen and fclose, but the first parameter of flock required a file descriptor of type int. The file pointer returned by fopen is converted to the file descriptor of type int (assuming that the file descriptor returned by the Open function is FD, and fopen returns the file pointer *fp, FD is equivalent to Fp->_fileno).
The following are examples of two processes:
#include
#include
#include
#include
int main (void)
{
FILE *FP = NULL;
int i = 20;
if (fp = fopen ("./file_lock.test", "r+b") = = = NULL)//Open File
printf ("File Open error!\n");
if (Flock (Fp->_fileno, lock_ex)! = 0)//Add lock to the file
printf ("File lock by others\n");
while (1)//Enter the loop, lock time is 20 seconds, print Countdown
{
printf ("%d\n", i--);
Sleep (1);
if (i = = 0)
Break
}
Fclose (FP); Exit after 20 seconds, close file
Flock (Fp->_fileno, lock_un); File unlock
return 0;
}
#include
#include
#include
#include
int main (void)
{
FILE *FP = NULL;
int i = 0;
if (fp = fopen ("./file_lock.test", "r+b") = = = NULL)//Open File
printf ("File Open error!\n");
Flock (Fp->_fileno, LOCK_EX); File lock
while (1)//Enter the loop
{
printf ("%d\n", i++);
Sleep (1);
}
Fclose (FP); Close File
Flock (Fp->_fileno, lock_un); Release file lock
return 0;
}
Run the file1.c first, then run the file2.c (run the file1.c 20 seconds after the file2.c will not see the phenomenon)
The phenomenon is: After file1.c executes, the countdown begins. Running file2.c at this point will block at the lock. After the file1.c has been running for 20 seconds to close the file and release the file lock, FILE2.C will start running.
Related reading:
Linux Multi-process Multithreading Mutex synchronization example http://www.linuxidc.com/Linux/2013-01/78394.htm
File lock and Python multi-process usage http://www.linuxidc.com/Linux/2012-08/69105.htm
Linux Multi-Process _ message Communication _ Design ideas Exchange http://www.linuxidc.com/Linux/2012-02/55454.htm
About synchronization and mutex http://www.linuxidc.com/Linux/2011-05/36704.htm between multiple processes (threads) in the Linux kernel
Linux Shell implements multi-process concurrency execution http://www.linuxidc.com/Linux/2011-03/33918.htm