Two File locks in Linux and two file locks in Linux

Source: Internet
Author: User
Tags flock lock

Two File locks in Linux and two file locks in Linux

A file lock is a file read/write mechanism that allows only one process to access one file at any specific time. Using this mechanism can make the process of reading and writing a single file more secure.

In this article, we will explore different types of File locks in Linux and use the example program to understand the differences between them.

We will take the following example to explain why file locks are needed.

1. process "A" to open and read A file, which contains information related to the account.

2. Process "B" also opened the file and read the information in the file.

3. Now, process "A" changes A balance record in its copy and writes it to A file.

4. At this time, process "B" does not know that the last read file has been changed, and it also saves the original file copy. The process "B" then changes the same record of the "A" operation and writes the record to the file.

5. At this time, only the records changed by process "B" are saved in the file.

To avoid such a problem, we need to use a file lock to ensure the "serialization" of the operation ".

 

The following are two common file locks in Linux:

1. Collaborative lock

Collaborative Locks require collaboration between processes involved in the operation. Assume that process "A" acquires a write lock and begins to WRITE content to the file. At this time, process "B" does not attempt to obtain A lock, it can still open the file and write content to the file. In this process, process "B" is a non-cooperative process. If process "B" tries to obtain a lock, the whole process is a cooperative process, which can ensure the "serialization" of the operation ".

Only when the process involved in the operation is collaborative, the collaborative lock can play a role. Collaborative locks are also called "unforced" locks.

 

2. Force lock

The process of force lock does not need to participate in the operation to maintain cooperation. It uses the kernel to check each open, read, and write operation to ensure that the file lock rules are not violated when these operations are called. For more information about force locks, refer to kernal.org.

To enable the Force lock function in Linux, you need to open it at the file system level and open it on a single file. The procedure is as follows:

1. When mounting a file system, use the "-o mand" parameter.

2. For the file lock_file to enable the Force lock function, you must enable the set-group-ID bit and disable the group-execute bit. (The reason for choosing this method is that when you disable group-execute, setting set-group-ID does not actually make sense)

 

Linux File lock example

To understand how File locks work, we create the program file file_lock.c:

include <stdio.h>#include <fcntl.h> intmain(intargc, char**argv) {  if(argc > 1) {    intfd = open(argv[1], O_WRONLY);    if(fd == -1) {      printf("Unable to open the file\n");      exit(1);    }    staticstruct flock lock;     lock.l_type = F_WRLCK;    lock.l_start = 0;    lock.l_whence = SEEK_SET;    lock.l_len = 0;    lock.l_pid = getpid();     intret = fcntl(fd, F_SETLKW, &lock);    printf("Return value of fcntl:%d\n",ret);    if(ret==0) {      while(1) {        scanf("%c", NULL);      }    }  }}

#

Compile this program with gcc:

# cc -o file_lock file_lock.c

Use the mount command with the "mand" parameter to remount the root file system, as shown below. This will enable the lock function at the file system level. Note: You must switch to the root user to execute the following command.

# mount -oremount,mand /

Create two files named advisory.txt.pdf and mandatory.txt In the executable directory (file_lock.pdf. Set-Group-ID can be enabled for “mandatory.txt, and Group-Execute-Bit cannot be enabled at the same time, as shown below:

# touch advisory.txt# touch mandatory.txt# chmod g+s,g-x mandatory.txt

Test collaborative lock: Execute the demo program, and use “advisory.txt as the parameter.

# ./file_lock advisory.txt

This program will wait for user input. From another terminal or console, try to enter the following command line:

# ls >>advisory.txt

In the example, lscommand writes the output to the advisory.txt file. Even if we get a write lock, some processes (not cooperating) can write data into the file. This is the so-called "collaborative" lock.

 

Test Force lock: Execute the demo program again, and use “mandatory.txt as the parameter.

# ./file_lock mandatory.txt

From another terminal or console, try to enter the following command line:

# ls >>mandatory.txt

In the example, the lscommand waits for the filelock to be deleted before writing its output to the mandatory.txt file. Although it is still a non-cooperative process, the Force lock plays a role.

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.