Create a lock file in Linux

Source: Internet
Author: User
Article Title: Create a lock file in a Linux operating system. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

  Overview

Linux provides multiple features for file locking. The simplest method is to create a lock file through atomic operations. The so-called "atomic operation" means that when a lock file is created, the system will not allow anything else to happen. This provides a way for the program to ensure that the file it creates is unique, and the file cannot be created by other programs at the same time.

  Method

Lock files act only as indicators and must be used by applications through mutual collaboration. The lock file only creates a lock, and the opposite is a mandatory lock.

To create a file used as an indicator, we use an open system call with the O_CREATE and O_EXCL labels. This will allow us to complete two tasks at the same time with an atomic operation: determine that the file does not exist, and then create it.

  Implementation

// File: lock. c

# I nclude

# I nclude

# I nclude

# I nclude

# I nclude

Int main ()

{

Int file_desc;

Int save_errno;

File_desc = open ("/tmp/LockFile. test", O_RDWR O_CREAT O_EXCL, 0444 );

If (file_desc <0)

{

Save_errno = errno;

Printf ("Open failed with error is % dn", save_errno );

}

Else {

Printf ("Open succeededn ");

}

Exit (EXIT_SUCCESS );

}

Run the program for the first time:

$ Lock

The output is as follows:

Open succeeded

Run the program again:

$ Lock

The output is as follows:

Open failed with error is 17

Analysis:

When the program is run for the first time, the execution is successful because the file does not exist. Subsequent execution fails because the file already exists. If you want the program to run successfully again, you must delete the lock file.

In Linux, the error number 17 usually indicates EEXIST, which indicates that a file already exists. The error code is defined in the header file errno. h or (more commonly) the header file it contains.

Related Article

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.