文件 Advisory Lock 笔记(Linux)

                     

贡献者: 待更新

   如果希望一个文件一次之被一个进程写入,且进程因任何原因中断时锁会自动释放,那么可以用 Advisory Lock(咨询锁/顾问锁),该锁由系统管理,所以即使进程崩溃也会解锁。

#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>

int main() {
    const char* filename = "example.txt";
    
    // Open the file for reading and writing;
    // create it if it doesn't exist
    int fd = open(filename, O_RDWR | O_CREAT, 0666);
    if (fd == -1) {
        std::cerr << "Error opening file: "
            << strerror(errno) << std::endl;
        return 1;
    }

    // Set up the file lock structure
    struct flock lock;
    memset(&lock, 0, sizeof(lock));
    lock.l_type = F_WRLCK; // Write lock
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;  // Start of the file
    lock.l_len = 0;  // Lock the whole file (0 means entire file)

    // Try to acquire the write lock
    if (fcntl(fd, F_SETLK, &lock) == -1) {
        std::cerr << "Unable to lock file: "
            << strerror(errno) << std::endl;
        close(fd);
        return 1;
    }
    
    std::cout << "File locked successfully."
        << "You can now read and write." << std::endl;
    
    // Write to the file
    const char* message = "Hello, world!\\n";
    if (write(fd, message, strlen(message)) == -1) {
        std::cerr << "Error writing to file: "
        << strerror(errno) << std::endl;
    }

    // Reading from the file (rewind to the beginning)
    lseek(fd, 0, SEEK_SET);
    char buffer[256];
    ssize_t bytesRead = read(fd, buffer, sizeof(buffer) - 1);
    if (bytesRead == -1) {
        std::cerr << "Error reading from file: "
            << strerror(errno) << std::endl;
    } else {
        buffer[bytesRead] = '\\0';
        std::cout << "Content of file:\\n" << buffer;
    }

    // Unlock the file
    lock.l_type = F_UNLCK;
    if (fcntl(fd, F_SETLK, &lock) == -1) {
        std::cerr << "Unable to unlock file: "
            << strerror(errno) << std::endl;
    } else {
        std::cout << "File unlocked successfully." << std::endl;
    }

    // Close the file descriptor
    close(fd);
    return 0;
}


致读者: 小时百科一直以来坚持所有内容免费无广告,这导致我们处于严重的亏损状态。 长此以往很可能会最终导致我们不得不选择大量广告以及内容付费等。 因此,我们请求广大读者热心打赏 ,使网站得以健康发展。 如果看到这条信息的每位读者能慷慨打赏 20 元,我们一周就能脱离亏损, 并在接下来的一年里向所有读者继续免费提供优质内容。 但遗憾的是只有不到 1% 的读者愿意捐款, 他们的付出帮助了 99% 的读者免费获取知识, 我们在此表示感谢。

                     

友情链接: 超理论坛 | ©小时科技 保留一切权利