/Cross-Compatible-FileLock-Windows-and-Linux

C++ class code that combines File Lock operations into a single class for Linux and Windows operating systems. The code in this repository is mostly for basic use.

Primary LanguageC++MIT LicenseMIT

Cross-Compatible File Lock for Linux and Windows Operating Systems

What is File Lock, and What Is It Used For?

File Lock is a synchronization mechanism that allows only one process to access a file at a time. It ensures proper ordering and synchronization between processes trying to access the same file, operating at the OS level rather than in memory. It is commonly used in databases, shared file access, log files, file management operations, and parallel programming.

Key Considerations

  • Long file locking periods can cause other processes to wait, potentially leading to performance issues.
  • File locks must be managed carefully on the client side. Failing to release locked files can lead to persistent issues in the system.

As the number of processes and their durations increase, the complexity of using File Lock can also increase. Therefore, it's essential to carefully evaluate your needs and your application's requirements before implementing File Lock.

Purpose of Code in This Repository

This project provides a cross-platform file locking mechanism for Linux and Windows using C++. It abstracts platform-specific details using the Strategy and Factory design patterns, offering a unified interface for file lock operations.

IMPORTANT!

The implementation waits for the file to be unlocked if it is already locked. You can modify the flags to issue an error instead and manage your process accordingly.

Documentation for File Lock Usage in Windows and Linux

For file locking operations in Windows operating systems, you need to include the "windows.h" header. For general usage methods and information about this library, please refer to the following documentation.

Windows Documentation - Learn Microsoft

Linux operating systems have multiple headers for file locking operations. Flock is used in the old period, Fcntl is new. For general usage methods and information regarding these contents, see the documents below.

Linux Documentation - GNU.org

Linux Documentation - Man Page

Features

  • Platform-specific file locking implementations (UnixFileLock for Linux and WindowsFileLock for Windows).
  • Factory pattern to abstract the creation of platform-specific file lock instances.
  • Modern C++ features like std::unique_ptr for memory management

Requirements

  • C++ compiler (e.g., g++ or Visual C++) (C++ 17 or higher)
  • CMake (at least version 3.0)
  • Git

Installation

To use the code, simply import the header and cpp files into your project as needed. If you want to try the basic example on the main.cpp function, you can follow these steps:

  1. Clone the repository:

    git clone https://github.com/KaganCanSit/Cross-Compatible-FileLock-Windows-and-Linux.git
    cd Cross-Compatible-FileLock-Windows-and-Linux
  2. Build the project using CMake:

    mkdir build
    cd build
    cmake ..
    make
  3. Run the executable:

    ./FileLockExample

Contribution

I have made an effort to create this repository thoughtfully, but errors may still occur. Please feel free to contribute and give constructive feedback.

If you would like to contribute, you can follow the CONTRIBUTING instructions.

License

This project is licensed under the MIT License. For details, see the LICENSE file.