- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
Why does forking my process cause the file to be read infinitely
(4 个回答)
1年前关闭。
故事
我试图诊断在 Linux 上用 C 编写的应用程序中的错误。原来这个bug是由引起的忘记 fclose
在子进程时FILE *
句柄在父进程中仍然打开。
文件操作只有read
.没有写操作。
情况1
应用程序在 Linux 5.4.0-58-generic
上运行.在这种情况下,错误发生了。
案例2
应用程序在 Linux 5.10.0-051000-generic
上运行.在这种情况下,有 没有错误 ,这正是我所期望的。
什么是错误?
父进程做fork
的随机数系统调用,如果没有 fclose
在子进程中。
案例2肯定
我完全清楚 忘记 fclose
会导致内存泄漏,但是:
exit(3)
不是 _exit(2)
. fclose
子进程影响父进程? 5.4
之后的版本中已修复的 Linux 内核错误。 .然而我没有证据,但我的测试告诉我。
fclose
来修复这个应用程序错误。在子进程退出之前。但是,我想知道在这种情况下实际发生了什么。所以我的问题是
怎么会忘记fclose
子进程影响父进程?
fclose
在子进程中。 test2.c 不调用
fclose
在子进程中。
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);
}
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
fclose
在 Linux 5.4.0-58-generic
上的子进程中导致父进程中的 fork 系统调用很奇怪。 Linux 5.10.0-051000-generic
上似乎不存在该错误. 最佳答案
感谢@Jonathan Leffler!
此问题与 Why does forking my process cause the file to be read infinitely 重复。
缺少的知识,为什么Linux 5.10.0-051000-generic
上没有出现bug原来它与内核无关。
原来,父进程与子进程竞争(与内核无关)。
fclose(3)
在子进程中,子进程将调用 lseek(2)
只要他们调用exit(3)
.这将导致父级重新读取相同的偏移量,因为子级调用 lseek(2)
负偏移 + SEEK_CUR
. lseek(2)
,这可能在@Jonathan Leffler 的回答中已经解释过了,我没有仔细阅读整个答案)。
lseek(2)
之前完成读取整个文件.那么就完全没有问题了。 Linux 5.10.0-051000-generic
上的父进程我用的机器只是一个
幸运过程在 child 调用
lseek(2)
之前总是先读取整个文件。 .
关于c - Linux x86-64 fork syscall 针对 C 标准 libc FILE I/O 的奇怪行为(关键字 : fork, fclose,linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65456297/
我看到这两个项目非常相关,但它们之间有什么区别?官方网页并没有说明太多。 我知道ABI(Application Binary Interface)用于在不同平台之间提供低级二进制接口(interfac
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
现代 glibc 二进制文件在 Linux 中称为 libc.so.6。为什么这里用“6”? libc.so.1 或 libc.so.8 也可以是好名字恕我直言。 维基百科在 http://en.wi
我在我的 Linux Mint 14 Nadia 中安装了 Matlab(a uname -a 显示:Linux Ideapad-Z570 3.5.0-17-generic#28-Ubuntu SMP
我有一个使用 eventfd 和 timerfd 内核系统调用的应用程序。为此,您需要一个现代内核和一个支持它们的 libc,至少是 2.8。 我目前的情况是,我的系统具有正确的内核,但是 2.7.1
我有一个用 musl libc 编译的共享库 $ readelf -d ./libinterop_d.so Dynamic section at offset 0x8ecb0 contains 22
我的问题是,是否总是需要 libc.a,即使我希望链接到 libc.so。我搜索了互联网,它给出了 3 个可能的答案(冲突) 1. c program will require to link to
我安装了多个版本的 libc,我如何在编译时选择要链接的对象? 现在我正在编译 g++ prog.cpp 最佳答案 您的程序将链接到 libc。当您在 gdb 中运行时,libc-dbg 将用于读取符
我在 Ubuntu 16.04 64 位上使用 gcc 5.4.0。当我编译程序时: gcc -o prog prog.c GCC 会自动链接到 C 标准库,因此我不必专门这样做。 我如何查看 gcc
我正在为我的系统安全类编写返回 libc 攻击。一、漏洞代码: //vuln.c #include #include int loadconfig(void){ char buf[1024];
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我正在尝试查找信息,但我没有在 clang 网站上看到它。我想尝试在 Windows 上使用它,但我不知道它是有自己的 libc 还是使用 MS 损坏的 libc? 另一个问题:如果我用 clang
我正在使用 clang 的规定通过插件和 clang 的 LibTooling 开发插件和工具。我能够做以下事情: 从 svn(Linux 和 OSX),通过运行配置脚本(不使用 cmake)按照入门
尝试运行时出现以下错误。 lib/libc.so.6:找不到(test.so)所需的版本“GLIBC_2.7” 要解决这个问题,在编译时静态链接到 libc-2.11.a 是否安全,这样在运行时就不会
首先,我在谷歌上搜索了很多,但没有找到与我的案例相关的任何东西,我有一个ELF可执行文件,我试图在我的Ubuntu WSL中运行它,我已经更改了权限(chmod+x文件),当我运行它时,显示以下错误。
GNU page说: Your program can arrange to run its own cleanup functions if normal termination happens.
我正在尝试在我的路由器上交叉编译 MIPSEL。我在汇编中找到了一些东西,但现在我开始尝试编译基本的 C 代码。 目前只有简单的 hello world c 代码,并使用 mipsel-linux-g
libc 中预期的字符编码是什么?例如,gethostname(char name, size_t namelen);以 char 作为参数。名称参数是否应该以 utf8(保持 ascii 完整)或纯
题 如果我包括 time.h ,更改“源类型参数”并重新包含该 header ,难道不应该添加这些新定义吗?我知道这是由于包括 guard 而发生的。我的问题是:这是 libc 中的错误吗?难道它不能
我正在学习Ubuntu上的动态链接器。我想导入我需要的libc函数,但是它链接了while libc.so文件。我不知道该怎么做,所以我来这里寻求帮助。 最佳答案 如果您将程序链接到动态库(例如 li
我是一名优秀的程序员,十分优秀!