fopen() 无法在 /tmp 共享上打开文件

Ale*_*lds 1 linux tmp fopen

我有一个 C 应用程序,它偶尔无法打开存储在/tmp共享上的文件。

这是相关的代码块:

  // open file and start parsing

  notStdin = strcmp(inFile, "-");
  if (notStdin) {
     coordsIn = fopen(inFile, "r");   <----- inFile = file that I want to open
     if (coordsIn == NULL) {
        fprintf(stderr, "ERROR: Could not open coordinates file: %s\n\t%s\n", inFile, strerror(errno));
        exit(EXIT_FAILURE);
     }
  }
  else
     coordsIn = stdin;
Run Code Online (Sandbox Code Playgroud)

在八到十次试验中,我得到一个NULLFILE 指针。这是一个示例错误消息:

ERROR: Could not open coordinates file: /tmp/coordinates.txt
       File or directory does not exist
Run Code Online (Sandbox Code Playgroud)

但是,该文件/tmp/coordinates.txt确实存在,因为我可以使用headcatmore等标准实用程序打开它。

不同/tmp/coordinates.txt试用文件的权限相同。

以下是结果uname -a

$ uname -a
Linux hostname 2.6.18-128.2.1.el5 #1 SMP Wed Jul 8 11:54:47 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

如果我使用inFile存储在不同的非/tmp共享中的不同的,则我不会观察到此症状。

是否有任何会导致fopen()存储在/tmp共享中的文件失败?我可以采取其他故障排除步骤吗?

Kyl*_*ndt 5

打开的文件太多?
您的程序是否打开了大量文件?也许你的文件描述符用完了?这是有关如何更改程序、外壳和操作系统的链接(如果是这种情况)。要查看您在程序中使用的许多内容:

sudo lsof | grep <PID> | wc -l
Run Code Online (Sandbox Code Playgroud)

在我的 Ubuntu 系统上,shell 限制为 1024,包括 stdout、stderr 和 stdin。这是在 /etc/security/limits.conf 中设置的。以下小程序显示了这一点:

#include <stdio.h>

int count=0;

int main( void ) {
    while(1) {
        FILE *fd = fopen("foo", "r");
        if ( fd == NULL) {
            printf("%i\n", count);
            return(1);
        }
        count++;
    }
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

当我运行时,它会打印“1021”,退出状态为 1。

检查系统错误:
更一般地说,您始终可以检查 dmesg 或 /var/log/messages 的输出是否有任何错误。

观察文件,看看是否有其他东西弄乱了它:
也许文件不存在,有什么东西从你下面删除了它?您可能希望使用 inotify 来监视文件上的所有事件,或者使用incroninotify-tools等使用inotify 的工具