简体   繁体   English

防止两次从不同进程打开文件

[英]Preventing opening file twice from different processes

After a research I made, I'm still not sure if there are APIs that allows you to open file exclusively, meaning, any other process would not be able to right to the file. 经过研究后,我仍然不确定是否有API允许您专门打开文件,这意味着任何其他进程都无法访问该文件。 Please can someone give me a good reference/guide/note/manual that covers this topic? 请有人能给我一个涵盖该主题的良好参考/指南/说明/手册吗?
Thanks a lot! 非常感谢!

Edit: Advisory Locking is good enough. 编辑:咨询锁定足够好。

There are three primary systems for file locking between processes: 进程之间有三种主要的文件锁定系统:

Some Unix-like systems might not have flock() ; 一些类Unix系统可能没有flock() ; they might have lockf() instead, for example, or they might only have fcntl() locking (though most will have both lockf() and fcntl() and many will have flock() too). 例如,它们可能具有lockf() ,或者它们可能仅具有fcntl()锁定(尽管大多数将同时具有lockf()fcntl()而许多也将具有flock() )。 The current version of POSIX mandates fcntl() locking and lockf() locking for file-level inter-process locking. 当前版本的POSIX要求fcntl()锁定和lockf()锁定用于文件级进程间锁定。 (POSIX also defines flockfile() , funlockfile() and ftrylockfile() — which are used for controlling locking between threads in an application). (POSIX还定义了flockfile()funlockfile()ftrylockfile() ,它们用于控制应用程序中线程之间的锁定)。

AFAIK, you can implement both lockf() and flock() locking using fcntl() locking. AFAIK,您可以使用fcntl()锁定来实现lockf()flock() fcntl()锁定。

Note that the locking functions work on file descriptors or file streams. 请注意,锁定功能适用于文件描述符或文件流。 Each program will be able to open the file, but will then apply advisory locking calls to check whether it has exclusive access to the file. 每个程序都可以打开文件,但是随后将应用咨询性锁定调用以检查它是否具有对该文件的独占访问权。

Note that some systems support mandatory file locking (indicated by setting the setgid bit on a file while the corresponding group execute bit is not set — so 2644 mode, for example). 请注意,某些系统支持强制性文件锁定(通过在未设置相应的组执行位的情况下在文件上设置setgid位来表示-例如2644模式)。 However, Mac OS X does not support mandatory locking (10.10 Yosemite tested, but prior versions also have this limitation). 但是,Mac OS X不支持强制锁定(已测试10.10 Yosemite,但以前的版本也有此限制)。 POSIX does not require mandatory locking support. POSIX不需要强制锁定支持。 It was provided on SVR4 systems. 它是在SVR4系统上提供的。

To prevent file reading from multiple processess, all of them must implement the same locking mechanism. 为了防止从多个进程读取文件,所有进程都必须实现相同的锁定机制。

One way would be using flock mechanism, which is not available everywhere. 一种方法是使用flock机制,但并不是到处都有。 From flock(2) manpage: flock(2)联机帮助页:

CONFORMING TO
    4.4BSD (the flock() call first appeared in 4.2BSD).
    A version of flock(), possibly implemented in terms of fcntl(2),
    appears on most UNIX systems.

Another way would be using a lockfile -- file alongside the original one indicating the lock is active. 另一种方法是使用锁文件 -与原始文件一起显示锁处于活动状态的文件。 C11 specifies a modifier x to mode in fopen , which ensures that the file is always created and not opened if it already exists: C11在fopen为模式指定了一个修饰符x ,以确保始终创建文件且如果文件已经存在则不打开:

FILE * f = fopen("filename.lock", "wx");
if (!f) {
    // File already exists!
    return 0;
}

// Do stuff
fclose(f);
remove("filename.lock");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM