I have this following scenario.
我有以下情况。
I create a pipe.
我创建了一个管道。
Forked a child process.
分叉儿童过程。
Child closes read end of the pipe explicitly and writes into the write end of the pipe and exits without closing anything ( exit should close all open file/pipe descriptors on behalf of the child, I presume).
Child显式关闭管道的读取结束并写入管道的写入端并退出而不关闭任何东西(退出应代表子代关闭所有打开的文件/管道描述符,我推测)。
Parent closes the write end of the pipe explicitly and reads from the read end of the pipe using fgets
until fgets
returns NULL. ie it reads completely.
Parent显式关闭管道的写入端,并使用fgets从管道的读取端读取,直到fgets返回NULL。即它完全读取。
Now my question is, why does the parent need to close the read end of the pipe explicitly once its done reading? Isn't it wise for the system to delete the pipe altogether once complete data has been read from the read-end?
现在我的问题是,为什么父母需要在读完后显式关闭管道的读取端?一旦从读取端读取完整数据,系统是否完全删除管道是不明智的?
I dint close the read end explicitly in the parent and I have Too many file descriptors
error sooner or later while opening more pipes. My assumption was that the system automatically deletes a pipe once its write end is closed and data has been completely read from read end. Cos you cant from a pipe twice!
我明确地在父级中关闭读取结束,并且在打开更多管道时迟早会出现太多文件描述符错误。我的假设是,一旦管道写入结束并且数据已从读取端完全读取,系统会自动删除管道。因为你不能两次管道!
So, whats the rationale behind the system not deleting the pipe once data has been completely read and write end closed?
那么,一旦数据完全读取并写入结束,系统背后的理由是不删除管道?
7
You're correct that the system will close the write end of the pipe once the child exits. However there could be another write end of that pipe open, if the child fork
s or passes a duplicate of the write end to another process.
你是正确的,一旦孩子退出,系统将关闭管道的写端。但是,如果子进程将fornd的副本分叉或传递给另一个进程,则可能会打开该管道的另一个写入结束。
It is still true that the system would be able to tell when all the descriptors at one end of a pipe have been closed (either explicitly or because the owning process exited). It still doesn't make sense to close those on the other end of the pipe, as that would lead to confusion when the parent process tries to close the descriptor on its end of the pipe; either:
系统仍然能够判断管道一端的所有描述符何时被关闭(显式或因为退出拥有过程),这仍然是正确的。关闭管道另一端的那些仍然没有意义,因为当父进程试图关闭管道末端的描述符时会导致混淆;之一:
fd已被系统关闭,在这种情况下,当它试图关闭已经关闭的fd时会出现错误;要么
fd已被重用,这更糟糕,因为它现在正在关闭一个完全不相关的fd。
From the point of view of the system, it might well have discarded the pipe once all the descriptors at one end have been closed, so you don't need to worry about inefficiency there. What matters more is that the user space process should have a consistent experience, which means not closing the descriptor unless it is specifically requested.
从系统的角度来看,一旦所有描述符都被关闭,它可能会丢弃管道,所以你不必担心那里的效率低下。更重要的是,用户空间进程应具有一致的体验,这意味着除非特别请求,否则不会关闭描述符。
5
File descriptors are not closed by the system, until the process exits. This is true for pipes, as well as any other file descriptor.
系统不会关闭文件描述符,直到进程退出。对于管道以及任何其他文件描述符都是如此。
There's a big difference between a pipe (or any other file) with no data in it and a closed file descriptor.
When a file descriptor is closed, the system can reuse its number for a new file descriptor. Then, when you read, you get something else. So after you've closed a file descriptor, you must no longer use it.
没有数据的管道(或任何其他文件)与封闭文件描述符之间存在很大差异。关闭文件描述符时,系统可以将其编号重用于新的文件描述符。然后,当你阅读时,你会得到别的东西。因此,在关闭文件描述符后,您必须不再使用它。
Now imagine that once there's no more data, the system would automatically close the file descriptor. This would make the number available for reuse, and a subsequent unrelated open may get it. Now the reader, who doesn't know yet that there's no more data, will read from what it thinks is the pipe, but will actually read from another file.
现在假设一旦没有更多数据,系统将自动关闭文件描述符。这将使该数量可供重用,并且随后的无关打开可以获得它。现在,那个还不知道没有更多数据的读者会从它认为是管道的内容中读取,但实际上会读取另一个文件。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2012/08/06/ee7fa7495855f6c0845d2e27cb82eefd.html。