SoFunction
Updated on 2025-04-07

Use C++ to implement cross-process safe file read and write lock

introduction

In multi-process systems, concurrent reading and writing of files may lead to data competition, file corruption and other problems. To ensure that multiple processes can access the same file safely, we need to use a file lock. C++ itself does not directly provide cross-process file locks, but we can use the file lock mechanism provided by the operating system (such as flock) to achieve cross-process safe file reading and writing.

This article will introduce how to implement file locks using C++ and ensure that the concurrent read and write operations of files are safe.

1. Basic concepts of file locks

File lock is a mechanism provided by the operating system to control process access to files. File locks ensure that files will be modified exclusively by one process at a certain moment, or multiple processes will be read in a shared manner, avoiding file corruption problems caused by concurrent read and write.

  • Shared lock (LOCK_SH: Allows multiple processes to read files at the same time, but does not allow writing.
  • Exclusive lock (LOCK_EX: Exclusively locked file, only one process can read and write the file, and other processes cannot read or write the file.

In C++, we can use system calls (such asflock) to implement file locking. The specific operating system implementation may vary, but generally speaking,flockProvides a simple and efficient way to manage concurrent access to files.

2. Use flock to implement file lock

In Linux environment, we can useflockThe function locks the file.flockis a system call that manages file locks and ensures secure access to files between multiple processes. By passing different flag parameters,flockShared locks or exclusive locks can be implemented.

2.1 Introduction to flock function

flockThe prototype of the function is as follows:

int flock(int fd, int operation);
  • fd: File descriptor.
  • operation: Lock operation, possible values ​​are:
    • LOCK_SH: Shared lock, multiple processes can acquire this lock for reading at the same time.
    • LOCK_EX: Exclusive lock, only one process can acquire this lock for reading and writing.
    • LOCK_UN: Unlock.

2.2 Basic structure of file lock implementation

We passedflockImplement a cross-process file lock. Here is a simple example showing how to implement a read-write lock for a file and ensure the security of multiple processes when accessing files.

#include <iostream>
#include <fstream>
#include <stdexcept>
#include <>
#include <sys/>

class FileLock {
public:
    // Constructor, open the file and add a lock    FileLock(const std::string& filename, bool writeLock = false)
        : m_fd(-1), m_fstream() {
        m_fd = open(filename.c_str(), O_RDWR);
        if (m_fd == -1) {
            throw std::runtime_error("Failed to open file for locking: " + filename);
        }

        // Select the lock method according to the parameters        int lockType = writeLock ? LOCK_EX : LOCK_SH;
        if (flock(m_fd, lockType) != 0) {
            close(m_fd);
            throw std::runtime_error("Failed to acquire file lock: " + filename);
        }

        // Open the file stream (for reading or writing)        m_fstream.open(filename, std::ios::in | std::ios::out);
        if (!m_fstream.is_open()) {
            flock(m_fd, LOCK_UN);  // Unlock            close(m_fd);
            throw std::runtime_error("Failed to open file stream: " + filename);
        }
    }

    // Destructor, unlock and close the file    ~FileLock() {
        if (m_fd != -1) {
            flock(m_fd, LOCK_UN);  // Unlock            close(m_fd);
        }
    }

    // Get file stream (read/write)    std::fstream& getFileStream() {
        return m_fstream;
    }

private:
    int m_fd;                 // File descriptor    std::fstream m_fstream;   // File read and write stream};

2.3 Code Description

  • ConstructorFileLockThe constructor of  will open the specified file and choose to use a shared lock based on whether it is necessary to write the lock (LOCK_SH) or exclusive lock (LOCK_EX). After the file locks successfully, we usestd::fstreamTo open the file stream and support subsequent read and write operations.

  • Destructor: In the destructor, we make sure to unlock the file and close the file descriptor to avoid resource leakage.

  • Get file stream:passgetFileStream(), you can obtain file stream objects to perform file read and write operations.

2.4 Use examples

int main() {
    const std::string filename = "";

    try {
        // Create a file lock and try to obtain the write lock        FileLock fileLock(filename, true);

        // Write through file stream        () << "This is a test message." << std::endl;

        std::cout << "File written successfully!" << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Error: " << () << std::endl;
    }

    return 0;
}

In this example, we first passFileLockGet the write lock of the file. Then, usestd::fstreamWrite the file. Because the file is written locked, other processes cannot access the file for reading or writing operations at this time, ensuring the security of the file.

3. Cross-process security and concurrency control

File lock (such asflock) is cross-process, which means that when one process locks a file, other processes are also affected by the lock, so that the file cannot be accessed. This ensures that file read and write operations between multiple processes are safe.

3.1 Shared lock and exclusive lock

  • Shared lock (LOCK_SH: Suitable for situations where multiple processes need to read files at the same time. Multiple processes can obtain shared locks to read files at the same time, but cannot modify the files.

  • Exclusive lock (LOCK_EX: Applicable to cases where a process needs to access files exclusively. Only processes holding exclusive locks can read and write files, and other processes cannot read or write to the file.

By rationally selecting read locks and write locks, the security of file read and write operations in a multi-process environment can be ensured.

3.2 Avoiding competition conditions

By using file locks, we avoid race conditions arising from multiple processes when modifying or reading files at the same time. Even if multiple processes try to access the same file, a file lock ensures that only one process can perform write operations within the same time, while other processes can only operate after the write lock is released.

4. Summary

In C++, use the operating system's file locking mechanism (such asflock), we can easily implement cross-process-safe file read and write locks. By using a shared lock (LOCK_SH) and exclusive lock (LOCK_EX), we can ensure the concurrency security of files in a multi-process environment, avoiding data corruption and race conditions. By combiningstd::fstream, we can read and write files efficiently.

This method is not only suitable for multi-process concurrent control in a stand-alone environment, but can also be extended to distributed systems, ensuring data consistency and security across processes through file locking mechanisms.

The above is the detailed content of using C++ to achieve cross-process safe file read and write locks. For more information about C++ file read and write locks, please pay attention to my other related articles!