Я обнаружил, что dotTrace Profiler от JetBrains является отличным инструментом профилирования для .NET, и их режим ASP.NET - это качество.
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).
Может быть, вы могли бы просто попытаться получить полную блокировку записи? Он потерпит неудачу, если кто-то еще откроет его для чтения или записи.
fopen("myfile.txt", "r+")
Если это не кроссплатформенный компьютер, а Win32, то вы можете запросить еще более детальный набор блокировок.
См. здесь ]
и посмотрите dwShareMode
, значение 0, а также другие параметры.
Нет. Если другое приложение не использует рекомендательные блокировки.
См. http://docs.sun.com/app/docs/doc/816-0213/6m6ne37v5?a=view
В крайнем случае, вы можете вызвать handle.exe Sysinternals ...
В Windows этот маленький и грязный трюк будет работать (если файл существует и у вас есть соответствующие права доступа)
if ( 0 != rename("c:/foo.txt", "c:/foo.txt") ) {
printf("already opened\n");
}
Вероятно, он также будет работать в Linux.
Как говорит @Neil Butterworth, стандартная библиотека этого не делает.
В unix вы можете использовать fcntl для блокировки файлов.
Вы можете написать оболочку для ваша открытая функция, которая проверяет наличие блокировки (и блокирует, если ее нет) файл, если он открыт никем другим. Вы также должны написать оболочку для close, которая снимает эту блокировку при закрытии файла.
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.