Linux x86-64 fork 系统调用对 C 标准 libc 文件 I/O 的奇怪行为关键字:fork、fclose、linux 案例 1案例 2什么是错误?案例2确认我目前的猜测:注意:test1.c 和 test2.c 的区别仅在于子进程中的 fclose test2.c 不会在子进程中调用 fclose文件 test.txt文件 test1.c文件 test2.c在 Linux 5.4.0-58-generic 上运行发生错误的地方看看test2执行bug

如何解决Linux x86-64 fork 系统调用对 C 标准 libc 文件 I/O 的奇怪行为关键字:fork、fclose、linux 案例 1案例 2什么是错误?案例2确认我目前的猜测:注意:test1.c 和 test2.c 的区别仅在于子进程中的 fclose test2.c 不会在子进程中调用 fclose文件 test.txt文件 test1.c文件 test2.c在 Linux 5.4.0-58-generic 上运行发生错误的地方看看test2执行bug

故事

我尝试在 Linux 上诊断用 C 编写的应用程序中的错误。事实证明,该错误是由于 fclose 句柄在父进程中仍处于打开状态时在子进程中忘记 FILE * 引起的。

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

案例 1

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

案例 2

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

什么是错误?

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

案例2确认

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

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

我目前的猜测:

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


问题

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


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

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

文件 test.txt

fclose

文件 test1.c

123123123
123123123
123123123
123123123
123123123
123123123

文件 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的随机数。

#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",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.10.0-051000-generic 上运行(相同的代码,完全没有错误)

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

总结

  • 忘记 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 会导致父进程中的 fork 系统调用变得奇怪。
  • 该错误似乎不存在于 Linux 5.4.0-58-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) 之前完成读取整个文件。那么就完全没有问题了。

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-