Search code examples
linuxconcurrencyiotransactionsfilesystems

How to synchronize file I/O of many independent applications on Linux?


I'm writing a software for Linux which would actively work with user's files in background concurrently with other applications that I don't control. I want to make my background application to not overwrite changes made by other applications. But there is a problem - unlike Windows Linux doesn't provide mandatory file locking capability which creates possibility of ruining user's work due to race conditions which I'd like to avoid.

So I wonder - are there file-systems available on Linux that provide some kind of synchronization mechanisms such as compare-and-swap operation, all-or-nothing transactions, mandatory file locking (like in Windows)?


Solution

  • I believe there are three possible solutions

    1) Make all programs to use a custom file I/O library that implement the features that you need. This solution may not be feasible if you do not have access to the source code. You may also consider to use mmap so that changes are written to memory. You use a background process to synchronize dirty pages to existing or new files.

    2) Replace standard C/C++ libraries (such as libc.so) that affected programs would use. You could use ldd to find out library dependency. You need to update source code for standard C/C++ to implement features that you need. This may be too difficult for most people.

    3) Create your file system. You may refer to many articles in the internet, such as https://kukuruku.co/post/writing-a-file-system-in-linux-kernel/. This is the best and cleanest solution.

    Hope it helps.