一旦两个文件描述符都关闭,Linux是否会自动释放未命名的管道?

pcd*_*623 5 c linux pipe

我使用一个未命名的管道进行父进程和通过fork()创建的子进程之间的进程间通信.我正在使用unistd.h中包含的pipe()函数

我假设一旦两个文件描述符都被关闭(并且在两个进程中),管道被释放/释放/销毁/等等.但我没有在手册中找到任何明确说明这一点的内容.我正在制作一个可以运行很长时间的程序,所以我想防止内存泄漏和其他类似的事情.

我的函数体看起来像:

int pipefds[2];

pipe( pipefds );

if ( fork() == 0 ) {

    close( pipefds[1] );
    ...
    //Use pipefds[0]
    close( pipefds[0] );

} else {

    close( pipefds[0] );
    ...
    //Use pipefds[1]
    close( pipefds[1] );
}
Run Code Online (Sandbox Code Playgroud)

是否可以安全地假设在此函数终止于子节点和父节点之后,管道已被释放/释放/销毁/等.?

有没有明确说明这一点的文件?

谢谢

Ste*_*sop 6

http://www.opengroup.org/onlinepubs/009695399/functions/close.html

当关闭与管道或FIFO特殊文件关联的所有文件描述符时,将丢弃管道或FIFO中剩余的任何数据.

实际上并没有所有资源都被释放了,因为内部内核gubbins不是"数据留在管道中",但我认为我们可以放心地假设如果你的内核在那之后保留了什么,这就是你内核的业务,而不是你的内核:-)


Ark*_*kku 5

文件close说这个.

 The close() call deletes a descriptor from the per-process object reference
 table.  If this is the last reference to the underlying object, the
 object will be deactivated.
Run Code Online (Sandbox Code Playgroud)