如何检查文件是否已被 C++ 中的另一个应用程序打开?
我知道,C++ 中有 is_open() 函数,但我希望一个程序检查文件是否尚未被另一个应用程序打开。 有没有办法使用标准库来做到这一点?
编辑 - 在答案中澄清这是针对 Linux 应用程序的。
I know, that there's the is_open()
function in C++, but I want one program to check if a file hasn't been opened by another application. Is there any way to do it using standard library?
EDIT - Clarified in the answers that this is for a Linux application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
不仅标准库没有这个功能,一般来说也是不可能的。 您可以(在 Linux 上)检查
/proc/*/fd
— 但您的程序可能无权在其他用户的进程上执行此操作(例如,这是 Ubuntu 中的默认设置) )。Not only the standard library does not have this funcionality, it's not even possible in general. You could (on linux) check
/proc/*/fd
— but it is possible that your program does not have permission to do it on processes from other users (this is the default in Ubuntu, for instance).不,标准库没有这样的功能。
No, the standard library has no such functionality.
如果您控制其他进程(有源代码),最好的计划是在两个进程中使用咨询锁。 这种锁定是在 POSIX 中定义的,并且可以跨操作系统移植。
在 Linux 中,您可以使用实用程序 lsof 来查看其他进程打开了哪些文件。
这仅限于您拥有的权限 - 您必须以特权用户身份进行检查,否则您只能获得与执行检查的用户相同的用户打开的文件的结果。
我只知道命令行实用程序,不知道任何可以直接从 C 代码使用的系统调用。
在 Linux 中,还可以为给定的文件系统打开强制锁定(mount - o 命令),并在文件上设置特殊标志(chmod gx,g+s)。 然后,当您的进程尝试获取写锁时,如果另一个进程打开了该文件,它将失败。 这几乎从未被使用过,但如果您完全控制有问题的系统,它可能是一个选择。
If you control the other process (have source code), the best plan is to use advisory locks in both processes. This locking is defined in POSIX, and will be portable across operating systems.
In Linux, you can use the utility lsof to see what files are opened by other processes.
This is limited to what you have permissions for - you have to do the check as a privileged user, or you'll only get results for files opened by the same user as the one doing the check.
I only know of the command line utility, not of any system call you can use directly from C code.
In Linux, it's also possible to turn on mandatory locking for a given filesystem (mount -o mand), and set special flags on the file (chmod g-x,g+s). Then when your process attempts to acquire a write lock, it will fail if another process has the file open. This is hardly ever used, but if you completely control the system in question, it may be an option.
以下代码可能有效。
The following code may work.
也许您可以尝试获得完全写锁? 如果其他人打开它进行读取或写入,它将失败。如果它不是跨平台并且是 Win32,那么您可以请求更细粒度的锁集。
请参阅此处
并查看 dwShareMode,值为 0,以及其他参数。
Perhaps you could just try and get a full write lock? It'll fail if anyone else has it open for reading or writing.If it's not cross platform and is Win32, then you can request even more fine-grained set of locks.
See here
and look at
dwShareMode
, value of 0, as well as the other parameters.没有。 除非其他应用程序使用咨询锁。
请参阅 http://docs.sun.com/app /docs/doc/816-0213/6m6ne37v5?a=视图
Nope. Unless other application uses advisory locks.
See http://docs.sun.com/app/docs/doc/816-0213/6m6ne37v5?a=view
非本机,您可以调用 Sysinternals 的handle.exe作为最后的手段...
Non-natively, you could call out to Sysinternals' handle.exe as a last resort...
正如 @Neil Butterworth 所说,标准库没有。
在unix中,您可以使用 fcntl 来使用文件锁。
您可以为 open 函数编写一个包装器,该包装器检查文件是否被其他人打开(如果不存在则锁定)。 您还应该为关闭编写一个包装器,以释放文件关闭时的锁定。
As @Neil Butterworth says, the standard library doesnt.
In unix you can use fcntl to use file locks.
You could write a wrapper for your open function, that checks for a lock (and locks if none exists) a file if its open by no one else. You shold write a wrapper for close as well, that releases that lock on file close.
看到你已经标记了 linux -> Linux 内核中还添加了一个命令行和 API 来执行此操作: inotify 。
这是 手册 页面。
Seeing you have tagged linux -> there's a command-line too and API that have been added to the Linux kernel and do just that: inotify.
Here's the man page.
在 Windows 中,这个脏小技巧将起作用(如果文件存在并且您具有正确的权限),
它也可能在 Linux 中起作用。
In Windows this little and dirty trick will work (if the file exists and you have the right permissions)
It's likely to work also in Linux.