无法锁定文件

时间:2016-10-20 15:20:04

标签: c++ linux filelock

FileLocker_wo.h

#include <string>

namespace Utils
{
        namespace FileLocker
        {
                bool lock_file(std::string aFileName, int& aFileDescriptor);

                bool unlock_file(int& aFileDescriptor);

                bool is_file_locked(std::string aFileName);
        };
}

FileLocker_wo.cpp

namespace Utils
{
        namespace FileLocker
        {
                bool lock_file(std::string aFileName, int& aFileDescriptor)
                {
                        aFileDescriptor = open(aFileName.c_str(), O_RDWR);

                        if (aFileDescriptor != -1)
                        {
                                if (lockf(aFileDescriptor, F_TLOCK, 0) == 0)
                                {
                                        return true;
                                }

                                std::cout << strerror(errno) << std::endl;
                        }

                        return false;
                }

                bool unlock_file(int& aFileDescriptor)
                {
                        if (lockf(aFileDescriptor, F_ULOCK, 0) == 0)
                        {
                                std::cout << "unloced file" <<  std::endl;
                                close(aFileDescriptor);
                                return true;
                        }
                        close(aFileDescriptor);
                        return false;
                }

                bool is_file_locked(std::string aFileName)
                {
                        int file_descriptor = open(aFileName.c_str(), O_RDWR);

                        if (file_descriptor != -1)
                        {
                                int ret = lockf(file_descriptor, F_TEST, 0);

                                if (ret == -1  && (errno == EACCES || errno == EAGAIN))
                                {
                                        std::cout << "locked by another process" << std::endl;
                                        close(file_descriptor);
                                        return true;
                                }

                                if (ret != 0)
                                {
                                    std::cout << "return value is " << ret << " " << strerror(errno) << std::endl;
                                }

                        }
                        close(file_descriptor);
                        return false;
                }
        }
}

p1.cpp

#include <iostream>
#include <fstream>

#include "FileLocker_wo.h"


int main()
{

        int fd = -1;
        if (Utils::FileLocker::lock_file("hello.txt", fd))
        {
                std::ofstream out("hello.txt");
                out << "hello ding dong" << std::endl;
                out.close();

                std::cout << "locked" << std::endl;
                sleep(5);
                if (Utils::FileLocker::unlock_file(fd))
                {
                        std::cout << "unlocked" << std::endl;
                }
        }

        return 0;
}

p2.cpp

#include "FileLocker_wo.h"
#include <iostream>
#include <fstream>

int main()
{
        int max_trys = 2;
        int trys = 0;
        bool is_locked = false;

        do
        {
                is_locked = Utils::FileLocker::is_file_locked("hello.txt");

                if (!is_locked)
                {
                        std::cout << "not locked" << std::endl;
                        break;
                }

                std::cout << "locked" << std::endl;

                sleep(1);
                ++trys;
        }
        while(trys < max_trys);

        if (!is_locked)
        {
                std::string s;
                std::ifstream in("hello.txt");
                while(getline(in,s))
                {
                        std::cout << "s is " << s << std::endl;
                }
        }

        return 0;
}

我试图在一个进程中获取文件锁,并在使用lockf(p1.cpp,p2.cpp)的其他进程中检查该文件是否有任何锁定。

在p1.cpp中,我锁定文件hello.txt并等待5秒钟。同时我启动p2.cpp并检查其他进程是否存在任何锁定,但始终没有锁定&gt;我坚持了最近2个小时。

有人可以告诉我这里有什么问题吗?

1 个答案:

答案 0 :(得分:3)

您在POSIX文件锁定中遇到了一个更糟糕的设计错误。您可能不知道这一点,因为您只阅读了lockf manpage,而不是fcntl manpage,所以这里是fcntl联机帮助页的重要位:

  
      
  • 如果进程关闭了引用文件的任何文件描述符,那么         无论如何,都会释放该文件上的所有进程锁         获取锁的文件描述符。
  •   

这意味着,在你的代码中

    if (Utils::FileLocker::lock_file("hello.txt", fd))
    {
            std::ofstream out("hello.txt");
            out << "hello ding dong" << std::endl;
            out.close();

当您拨打out.close()时,即使 out是不同的操作系统级别&#34;打开文件说明&#34;比你在lock_file中使用的那样!

为了安全地使用POSIX锁定,您必须确保在每个进程上一次> 的文件上调用open(),您必须永远不要复制文件描述符,并且你必须在准备放下锁时才能再次关闭它。因为可能没有任何方法(甚至使用不可移植的扩展)从文件描述符构造iostreams对象,或者从iostreams对象中提取文件描述符,所以阻力最小的路径是仅使用 操作系统级I / O原语(openclosereadwritefcntllseekftruncate)使用POSIX锁定到的文件。