【问题标题】:Linux x86-64 fork syscall strange behavior against C standard libc FILE I/O (keywords: fork, fclose, linux) [duplicate]Linux x86-64 fork syscall 针对 C 标准 libc FILE I/O 的奇怪行为(关键字:fork、fclose、linux)[重复]
【发布时间】:2021-04-03 23:12:03
【问题描述】:

故事

我试图诊断在 Linux 上用 C 语言编写的应用程序中的错误。原来,该bug是由于父进程中FILE *句柄仍处于打开状态时,子进程中忘记fclose引起的。

文件操作只有read。没有写操作。

案例一

应用程序正在Linux 5.4.0-58-generic 上运行。在这种情况下,错误发生了。

案例2

应用程序正在Linux 5.10.0-051000-generic 上运行。在这种情况下,没有错误,这是我所期望的。

什么是错误?

如果子进程中没有fclose,父进程会随机调用fork 系统调用。

案例2确认

我完全知道忘记fclose会导致内存泄漏,但是:

  • 我认为,只是在这种情况下,并不是绝对必要的,因为子进程要尽快退出,而我使用的退出是exit(3)而不是_exit(2) .
  • 奇怪的是,子进程中的忘记fclose怎么会影响父进程?

我目前的猜测:

这是一个Linux内核错误,在5.4之后的版本中已经修复。然而我没有证据,但我的测试证明了。


问题

我已经能够通过在子进程退出之前调用fclose 来修复此应用程序错误。但是,我想知道在这种情况下实际发生了什么。所以我的问题是为什么在子进程中忘记fclose 会影响父进程?


重现问题的非常简单的代码(附 3 个文件)。

注意:test1.c 和 test2.c 的区别仅在于子进程中的fclose。 test2.c 不会在子进程中调用fclose。

文件 test.txt

123123123
123123123
123123123
123123123
123123123
123123123

文件 test1.c

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#define TICK do { putchar('.'); fflush(stdout); } while(0)
int main() {
  char buff[1024] = {0};
  FILE *handle = fopen("test.txt", "r");

  uint32_t num_of_forks = 0;

  while (fgets(buff, 1024, handle) != NULL) {

    TICK;
    num_of_forks++;

    pid_t pid = fork();
    if (pid == -1) {
      printf("Fork error: %s\n", strerror(errno));
      continue;
    }

    if (pid == 0) {
      fclose(handle);
      exit(0);
    }
  }

  fclose(handle);
  putchar('\n');
  printf("Number of forks: %d\n", num_of_forks);
  wait(NULL);
}

文件 test2.c

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#define TICK do { putchar('.'); fflush(stdout); } while(0)
int main() {
  char buff[1024] = {0};
  FILE *handle = fopen("test.txt", "r");

  uint32_t num_of_forks = 0;

  while (fgets(buff, 1024, handle) != NULL) {

    TICK;
    num_of_forks++;

    pid_t pid = fork();
    if (pid == -1) {
      printf("Fork error: %s\n", strerror(errno));
      continue;
    }

    if (pid == 0) {
      // fclose(handle);
      exit(0);
    }
  }

  fclose(handle);
  putchar('\n');
  printf("Number of forks: %d\n", num_of_forks);
  wait(NULL);
}


运行程序


在 Linux 5.4.0-58-generic 上运行(发生错误的地方)

看test2执行(bug),它导致fork syscall的随机数。

ammarfaizi2@integral:/tmp$ uname -r
5.4.0-58-generic
ammarfaizi2@integral:/tmp$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ammarfaizi2@integral:/tmp$ ldd --version
ldd (Ubuntu GLIBC 2.31-0ubuntu9.1) 2.31
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.
ammarfaizi2@integral:/tmp$ cat test.txt
123123123
123123123
123123123
123123123
123123123
123123123
ammarfaizi2@integral:/tmp$ diff test1.c test2.c
27c27
<       fclose(handle);
---
>       // fclose(handle);
ammarfaizi2@integral:/tmp$ gcc test1.c -o test1 && gcc test2.c -o test2
ammarfaizi2@integral:/tmp$ ./test1
......
Number of forks: 6
ammarfaizi2@integral:/tmp$ ./test1
......
Number of forks: 6
ammarfaizi2@integral:/tmp$ ./test1
......
Number of forks: 6
ammarfaizi2@integral:/tmp$ ./test2
..................................................................................................................................................................................
Number of forks: 178
ammarfaizi2@integral:/tmp$ ./test2
............................................................................................................................................................................................................................................................................................................................................................
Number of forks: 348
ammarfaizi2@integral:/tmp$ ./test2
...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Number of forks: 475
ammarfaizi2@integral:/tmp$ md5sum test1 test2
c32d03916b9b72546b966223837fd115  test1
f314d2135092362288a66f53b37ffa4d  test2

在 Linux 5.10.0-051000-generic 上运行(相同的代码,完全没有错误)

root@esteh:/tmp# uname -r
5.10.0-051000-generic
root@esteh:/tmp# gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@esteh:/tmp# ldd --version
ldd (Ubuntu GLIBC 2.31-0ubuntu9.1) 2.31
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.
root@esteh:/tmp# cat test.txt
123123123
123123123
123123123
123123123
123123123
123123123
root@esteh:/tmp# diff test1.c test2.c
27c27
<       fclose(handle);
---
>       // fclose(handle);
root@esteh:/tmp# gcc test1.c -o test1 && gcc test2.c -o test2
root@esteh:/tmp# ./test1
......
Number of forks: 6
root@esteh:/tmp# ./test1
......
Number of forks: 6
root@esteh:/tmp# ./test1
......
Number of forks: 6
root@esteh:/tmp# ./test2
......
Number of forks: 6
root@esteh:/tmp# ./test2
......
Number of forks: 6
root@esteh:/tmp# ./test2
......
Number of forks: 6
root@esteh:/tmp# md5sum test1 test2 # Make sure the files are identical with case 1
c32d03916b9b72546b966223837fd115  test1
f314d2135092362288a66f53b37ffa4d  test2

总结

  • 在Linux 5.4.0-58-generic 的子进程中忘记fclose 会导致父进程中的fork 系统调用异常。
  • Linux 5.10.0-051000-generic 上似乎不存在该错误。

【问题讨论】:

  • 子进程中的文件描述符应该独立于父进程中的文件描述符 这是错误的。毕竟,跨叉的 FD 是同一个 FD 的不同句柄,就像您使用 dup(2) 或 dup2(2) 得到的一样。只有关闭(和复制)是独立的,当你从一个FD读/写/lseek(2)时,另一个FD也会受到影响。
  • @iBug 感谢您的更正。我想澄清一些事情,以确保我能在更高层次上理解您的评论(从 man 3 xxxx 的 utils 中查看)。 -- 所以,如果我用fopen(3)打开一个文件,然后我调用fork(2),然后我用fseek(3)改变file handle which is created by the parent process的偏移量子,那么父文件句柄的偏移量将根据我在子进程中执行的fseek(3) 调用而改变。对吗?
  • 是的,完全正确。但请记住,进程调度可能会使结果不可预测,除非您实施某种“同步”。
  • 在每个系统上提供 glibc 运行时版本。
  • 我认为您的问题可能与Why does forking my process cause the file to be read infinitely?中分析的问题密切相关,如果不是相同的话

标签: c linux fork x86-64 stdio


【解决方案1】:

感谢@Jonathan Leffler!

这个问题与Why does forking my process cause the file to be read infinitely重复

缺失的知识,为什么Linux 5.10.0-051000-generic上没有出现bug,原来是和内核无关。


原来是父进程与子进程竞争(与内核无关)。

  • 注意:如果句柄是由父进程创建的,从子进程更改文件句柄的偏移量也会改变父进程中的偏移量>.
  • 如果子进程中没有fclose(3),子进程将在调用exit(3) 后立即调用lseek(2)。这将导致父级重新读取相同的偏移量,因为子级调用 lseek(2) 时带有负偏移量 + SEEK_CUR。

(我不知道为什么要在退出前调用lseek(2),可能在@Jonathan Leffler 的回答中已经解释过了,我没有仔细阅读整个答案)。

  • 如果父级在子级调用lseek(2) 之前完成读取整个文件。那么就完全没有问题了。

另外,正如@iBug 提到的那样但请记住,进程调度可能会使结果不可预测,除非您实施某种“同步”。

我使用的Linux 5.10.0-051000-generic机器上的父进程只是一个幸运进程,它总是在孩子调用lseek(2)之前先读取整个文件。

我尝试在文件中添加更多行(为 150 行),因此父级通常会比读取 6 行要慢,并且未定义的行为会发生。

测试结果:https://gist.githubusercontent.com/ammarfaizi2/b72bd03fcc13779f96b8bbeef9253e66/raw/da1eff4ed5434aa51929e5c810d54de8ffe15548/test2_fix.txt

【讨论】:

  • 为什么 lseek 会发生:这是fflush 的一个不幸部分,在 fflush 的一般情况下需要它,但在退出时可能不需要。 (除非坚持某些规范要求的字母,例如,如果exit(3) 按名称指定fflush(3)。)是的,我在乔纳森的优秀答案的底部找到了这一点。解决方法是fflush(stdin) before 分叉,因此 exit 发现 stdin 缓冲区为空(因此 Unix fd 位置与 stdio 读取位置同步,因此不需要 lseek)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-31
  • 2020-09-03
  • 1970-01-01
  • 2016-02-19
相关资源
最近更新 更多