FLOCK() FUNCTION IN C++
In this article, you will learn about the flock() function in C++ with its example.
Introduction:
File locking in C++ is handled by the flock() function, a Unix-specific system call, rather than a standard C++ library function. Primarily, it is utilized to regulate simultaneous file access by several programs, ensuring that a single process can alter a file at any given moment to avoid data corruption or discrepancies.
The <fcntl.h> header contains the flock() function, which is commonly utilized on Unix-based operating systems like Linux and macOS. It offers advisory file locking, which does not completely stop other programs from accessing the file but does warn them to respect the lock. Since cooperating processes have the option to disregard the advisory lock, it is imperative that they adhere to the same procedure.
- The required header file should be included.( #include <fcntl.h>)
- Using either fstream or open(), depending on your needs, open a file for writing or reading.
- If you want to lock the file, use the flock() function. You can utilize one of two kinds of locks:
- Shared lock (LOCK_SH): It permits several processes to read a file at once while blocking writing to it.
- Exclusive lock, LOCK_EX: It prevents reading or writing of the file by any other processes.
Example 1:
C++ code for flock() function in opening and closing:
#include <iostream> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <sys/file.h> void lockUnLock() { int fd, i; char path[] = "test.txt"; fd = open(path, O_WRONLY | O_CREAT); if (fd != -1) { std::cout << "open file " << path << std::endl; std::cout << "Please input a number to lock the file" << path << std::endl; scanf("%d", &i); if (flock(fd, 2) == 0) { std::cout << "The file was locked " << std::endl; } else { std::cout << "The file was not locked " << std::endl; } std::cout << "please input a number to unlock the file " << std::endl; scanf("%d", &i); if (flock(fd, 8) == 0) { std::cout << "The file was unlocked. " << std::endl; } else { std::cout << "The file was no unlocked. " << std::endl; } close(fd); } else { std::cout << "Cannot open file " << path << std::endl; } } int main() { lockUnLock(); return 0; }
Output:
open file test.txt Please input a number to lock the filetest.txt tutorial.txt The file was locked please input a number to unlock the file The file was unlocked.
Example 2:
Let's take another program to illustrate the flock() function in C++.
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <sys/file.h> #include <iostream> void lockUnLock() { int fd, i; char path[] = "test.txt"; fd = open(path, O_WRONLY | O_CREAT); if (fd != -1) { std::cout << "open file " << path << std::endl; std::cout << "Please input a number to lock the file" << path << std::endl; scanf("%d", &i); if (flock(fd, 2) == 0) { std::cout << "The file was locked " << std::endl; } else { std::cout << "The file was not locked " << std::endl; } std::cout << "please input a number to unlock the file " << std::endl; scanf("%d", &i); if (flock(fd, 8) == 0) { std::cout << "The file was unlocked. " << std::endl; } else { std::cout << "The file was no unlocked. " << std::endl; } close(fd); } else { std::cout << "Cannot open file " << path << std::endl; } } int main() { lockUnLock(); return 0; }
OUTPUT:
open file test.txt Please input a number to lock the filetest.txt flock().txt The file was locked please input a number to unlock the file The file was unlocked.
Explanation:
In this code,
- We use the open() function to open "file.txt" and retrieve a file descriptor.
- Using the flock() function, we impose an exclusive lock to stop concurrent writes to the file by other processes.
- The program waits for the user to press Enter to release the lock.
- It leaves and closes the file after unlocking the lock.
- Kindly make sure that the program is located in the same directory as the "file.txt" file, or supply the path to the file you wish to lock.
Conclusion:
File locking in C and C++ programs is accomplished via the flock() function, a Unix-specific system call. Applications can obtain advisory locks on files to manage many users' access concurrently and safeguard the integrity of the data. A simple method for coordinating file access across several processes is the flock() function, which allows shared or exclusive locks to be applied. The important thing to keep in mind is that flock() is advisory, which means that not all file systems and operating systems fully support it, and it only recommends that other processes respect the lock without enforcing it. Because of this, other techniques like std::mutex from the C++ Standard Library are frequently selected for more reliable cross-platform file locking and synchronization in C++ programs.