读取文件时正在创建不需要的子进程 [英] Unwanted child processes being created while file reading

查看:99
本文介绍了读取文件时正在创建不需要的子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个多进程程序.当我尝试使用if(f == 0) break;在for循环中调用fork()时.我得到了所需数量的子进程.

I am creating a multi process program. When I tried to call fork() in a for loop using if(f == 0) break;. I got the desired number of child processes.

但是,现在我正在处理输入文件,并且最初不知道所需的进程数.这是我的代码的最小示例.

However now, I am dealing with an input file, and the desired number of processes is not known initially. Here is the smallest possible example of my code.

FILE* file = fopen("sample_input.txt", "r");
while(fscanf(file, "%d", &order) == 1){      
    f = fork();
    if(f == 0){
        break;
    } 
}

示例sample_input.txt:

5 2 8 1 4 2

现在正在创建数千个子进程(我想要6,即文件中整数的数量),这可能是什么原因?与文件指针有关吗?

Now thousands of child processes are being created (I want 6, the number of integers in the file), what could be the reason ? Is it something to do with the file pointer ?

编辑:我使用控制台输出进行了一些调试,子进程确实跳出了循环.但是,父母不断重复读取一个小文件.如果删除fork(),则循环将按预期执行6次.

I did some debugging with console outputs, the child processes are indeed breaking out of the loop. However the parent keeps reading a small file over and over. If I remove fork(), the loop executes 6 times as intended.

Edit2 :我有一个理论,我无法证明也许您可以帮助我.可能是这样的情况:进程之间共享文件指针,当子进程退出时,它将关闭文件,而当父进程再次尝试读取时,它只是从头开始(或其他怪异的行为).可能是这样吗?

I have a theory, I can't prove it maybe you can help me. It could be the situation that the file pointer is shared between processes, when a child exits, it closes the file and when the parent tries to read again, it just starts from the beginning (or some other weird behavior). Could it be the case ?

推荐答案

当第一个进程读取第一个数字时,它实际上将整行读取到内存中.该过程分叉.

When the first process reads the first number, it actually reads the whole line into memory. The process forks.

子进程中断循环;接下来没有发生什么,但可能会退出.父进程现在读取第二个数字,然后再次进行分叉.再次,孩子退出,父母读取第三个数字,叉子等.

The child process breaks the loop; what happens next is not specified, but it probably exits. The parent process now reads the second number and forks again. Again, the child exits and the parent reads the third number, forks, etc.

读取第六个数字并退出第六个子级后,父级将从文件中读取另一个缓冲区.在Linux上(或更确切地说,在GNU C库中),您会得到一些奇怪的效果.请参阅中的讨论为什么派生我的进程会导致无限读取文件?以查看详细信息.但是,退出的孩子将文件描述符的读取位置调整回开始,因此父文件可以再次读取更多数据.

After the sixth number is read and the sixth child exits, the parent goes to read another buffer from the file. On Linux (or, more precisely, with the GNU C Library), you then get some weird effects. See the discussion in Why does forking my process cause the file to be read infinitely? to see the details. However, the children exiting adjust the read position of the file descriptor back to the start, so the parent can read more data again.

我对另一个问题的回答表明,如果子进程在退出前关闭文件,则不会发生此现象. (无论如何都不应该发生,但是凭经验可以.)

My answer to the other question shows that if the child processes close the file before exiting, this behaviour does not occur. (It shouldn't occur anyway, but it does, empirically.)

GLIBC 错误23151 -包含未关闭文件的分叉进程之前无法执行退出并可能导致父I/O无限循环.

GLIBC Bug 23151 - A forked process with unclosed file does lseek before exit and can cause infinite loop in parent I/O.

该错误创建于2019-05-08美国/太平洋地区,并于2018-05-09被关闭为INVALID.给出的原因是:

The bug was created 2019-05-08 US/Pacific, and was closed as INVALID by 2018-05-09. The reason given was:

请阅读 http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_05_01 , 尤其是本段:

Please read http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_05_01, especially this paragraph:

请注意,在fork()之后,存在两个句柄,其中一个以前存在. […]

Note that after a fork(), two handles exist where one existed before. […]

请参阅为什么分叉我的进程会导致无限读取文件?对此进行广泛的讨论.

Please see Why does forking my process cause the file to be read infinitely? for an extensive discussion of this.

这篇关于读取文件时正在创建不需要的子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆