boost interprocess file_lock 不适用于多个进程
我似乎遇到了 boost::interprocess::file_lock 的问题,
我有进程 1,本质上是
boost::interprocess::file_lock test_lock("testfile.csv");
test_lock.lock();
sleep(1000);
test_lock.unlock();
当我在第一个进程处于睡眠状态时运行第二个进程时,我发现我仍然能够读取 testfile.csv。更糟糕的是,我什至可以覆盖它。
我是否误解了 file_lock 的工作原理?我的印象是,调用 .lock() 会赋予它对文件的独占锁定,并防止任何其他进程读取/修改该文件。
I seem to be having an issue with boost::interprocess::file_lock
I have process 1 that is essentially
boost::interprocess::file_lock test_lock("testfile.csv");
test_lock.lock();
sleep(1000);
test_lock.unlock();
When I run a second process while the first process is sleeping, I find that I am still able to read testfile.csv. What's worse, I can even overwrite it.
Am I misinterpreting how file_lock works? I was under the impression that calling .lock() gives it exclusive lock over the file and prevents any other process from read/modifying the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
file_lock 不是用于锁定文件。它是一个使用文件作为其支持技术的互斥对象。文件的内容基本上是不相关的;相关的是指向该文件的 file_lock 的所有实例都将遵循该锁的锁定特性。
与任何互斥类型对象一样,锁本身是为了保护或以其他方式计量对某些其他资源的访问。
它与文件的文件系统保护无关。
参考
file_lock is not for locking a file. It is a mutual exclusion object that uses a file as its backing technology. The contents of the file are basically irrelevant; what is relevant is that all instances of a file_lock pointing to that file will respect the locking characteristics of the lock.
As with any mutex-type object, the lock itself is there to protect or otherwise meter access to some other resource.
It has nothing to do with filesystem protection of files.
Reference
为了确保可移植性,Boost 中不存在您所期望的那种锁。 boost 支持的 Unix/Linux/OSX 操作系统上没有内核级强制锁。
请参阅:
http://www.boost.org/doc/libs/1_51_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.file_lock
因此,boost 进程间锁是咨询性或合作性的锁。您可以使用 boost::interprocess::file_lock 完成您尝试执行的操作,但是,您还必须在可能尝试读/写文件的其他进程中使用 interprocess::file_lock。只需在访问文件之前尝试获取其他进程中的锁即可。
这就是 interprocess::file_lock 的设计用途。如果您使用某些操作系统,您可以使用某种操作系统特定的方式来强制执行内核级锁定,但如果您使用 boost::interprocess::file_lock,您的代码将是可移植的。
To ensure portability the kind of lock you are expecting doesn't exist in boost. There is no kernel level enforceable lock on the Unix/Linux/OSX OS's that boost supports.
see:
http://www.boost.org/doc/libs/1_51_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.file_lock
Because of this the boost interprocess lock is an advisory or cooperative lock. You can accomplish what you are trying to do with boost::interprocess::file_lock but, you must also use the interprocess::file_lock in other processes that might try to read/write the file. Simply try to acquire the lock in your other process before accessing the file.
This is how interprocess::file_lock was designed to be used. You can do it some OS specific way that enforces kernel level locking if you are on some OS's, but if you use the boost::interprocess::file_lock, your code will be portable.
它不是文件系统锁。
简单地锁定一个进程并不能阻止另一进程访问该文件。把它想象成“像”一个互斥锁。要让第二个进程遵守 file_lock,您还需要在第二个进程中获取锁。然后你会看到proc2将阻塞,等待proc1释放锁。
Its not a filesystem lock.
Simply locking in one process is not going to prevent another process from accessing the file. Think of it "like" a mutex. To have your second process honor the file_lock, you need to acquire the lock in the 2nd process as well. Then you will see that proc2 will block, waiting for proc1 to release the lock.