【发布时间】: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?中分析的问题密切相关,如果不是相同的话