fopen 不返回

发布于 2024-10-08 11:01:33 字数 606 浏览 0 评论 0原文

我在 C 程序中使用“fopen”以只读模式 (r) 打开文件。但就我而言,我观察到 fopen 调用没有返回。它不返回 NULL 或有效指针 - 执行在 fopen 调用时被阻止。文件补丁绝对正确(我已经验证过)并且不存在与权限相关的问题。任何人都可以告诉我们这种行为的原因是什么。任何形式的帮助都是非常值得赞赏的。有没有与 gcc 或 glibc 相关的东西?

编辑

这是示例代码

printf("%s %d\n",__FUNCTION__,__LINE__);
if ((fp = fopen(argv[1], "r")) == NULL) {
   printf("%s %d\n",__FUNCTION__,__LINE__);
   return;
}
printf("%s %d\n",__FUNCTION__,__LINE__);

当我运行此代码时,我只得到第一个打印(在调用 fopen 之前),并且在该程序停止之后。所以 fopen 没有完成它的操作。该文件是一个带有“.conf”扩展名的简单配置文件,可以通过 vi、cat 等所有其他方式打开该文件。不应该有任何 NFS 相关问题。文件系统是ext3。

提前致谢, 苏维克

I used 'fopen' in a C program to open a file in readonly mode (r). But in my case I observed that fopen call does not return. It does not return NULL or valid pointer - execution gets blocked at fopen call. The patch of file is absolutely correct (I have already verified that) and there is no permission related issues. Can anybody please tell what could be the reason for this kind if behavior. Any kind of help is really appreciable. Is there anything related to gcc or glibc?

EDIT

Here is the sample code

printf("%s %d\n",__FUNCTION__,__LINE__);
if ((fp = fopen(argv[1], "r")) == NULL) {
   printf("%s %d\n",__FUNCTION__,__LINE__);
   return;
}
printf("%s %d\n",__FUNCTION__,__LINE__);

When I run this code, I only get the first print (before calling fopen) and after that program just halts. So fopen does not complete it's operation. The file is a simple configuration file with '.conf' extension and this file can be opened by all other means like vi, cat etc. There should not be any NFS related issue. Filesystem is ext3.

Thanks in advance,
Souvik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心房敞 2024-10-15 11:01:33

有几个原因:

  • 您在某处损坏了内存,并且所有关于发生的事情的赌注都已关闭(通过 valgrind 运行您的程序)
  • 您在信号处理程序中调用此代码, fopen() 不是信号异步安全的,所以真的任何事情都可能发生(虽然由于 FILE* 内部互斥锁而导致死锁很常见)
  • 该文件是 fifo< /a> ,在这种情况下,打开文件将被阻止,直到有人在另一端打开该文件(读/写)
  • 该文件位于陈旧的 NFS 挂载上。
  • 该文件是一个字符/块特殊文件,其语义是打开块直到发生有趣的事情,

Here's a few reasons:

  • You've corrupted memory somewhere, and all bets are off as to what's happening (run your program through valgrind)
  • You're calling this code inside a signal handler, fopen() is not signal async safe, so really anything could happen (a deadlock due to the FILE* internal mutex is common though)
  • The file is a fifo , in which cases opening the file will block until someone opens the file at the other end(read/writing)
  • The file is on a stale NFS mount.
  • The file is a character/block special file with semantics that open blocks until something interesting happens,
姐不稀罕 2024-10-15 11:01:33

所以呢? fopen 可以阻塞,直到文件被打开,或者直到确定访问被拒绝为止。如果您的存储设备速度较慢,那么等待它可用是绝对正确的。但这是操作系统的问题,而不是 C 的问题。

So what? fopen is allowed to block until the file has been opened, or until it has been determined that access is denied. If you have a slow storage device, it is absolutely correct to wait until that becomes available. But that is an operating system issue then, not C's.

不即不离 2024-10-15 11:01:33

我注意到,如果成功打开文件,则不会关闭该文件。

您是否有可能之前运行过它并杀死了它,现在您有一个进程打开并锁定了文件?

如果是这样,那么 fopen 可能正在等待锁被释放。

I notice you don't close the file if you open it successfully.

Is it possible you that you have run it before and killed it, and now you have a process out there which has the file open, and locked?

If so, then maybe fopen is waiting for the lock to be released.

紫﹏色ふ单纯 2024-10-15 11:01:33

您是否有可能在保留的命名空间中重新定义了符号:以两个下划线、一个下划线和一个大写字母开头的符号,或者任何标准 C 库函数?如果是这样,就会导致未定义的行为,并且 fopen 可能会以某种方式最终调用部分代码而不是标准库中的正确代码。

这个问题有一种严重的“信息缺失”的味道。我严重怀疑问题中的代码片段是否具有 OP 在 main 中单独出现时所描述的行为,我想知道 OP 是否没有做过一些他没有告诉我们的虚假事情......

Is it possible that you've redefined a symbol in the reserved namespace: either something beginning with two underscores, an underscore and a capital letter, or any of the standard C library functions? If so, that results in undefined behavior, and it's possible that fopen somehow ends up calling part of your code instead of the correct code in the standard library.

This question has a major "missing information" smell to it. I seriously doubt the code snippet in the question has the behavior OP has described when it appears by itself in main, and I wonder if OP hasn't done some bogus stuff he's not telling us about...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文