在C语言中,子级关闭的文件描述符在父级中也关闭了吗? [英] In C, are file descriptors that the child closes also closed in the parent?

查看:170
本文介绍了在C语言中,子级关闭的文件描述符在父级中也关闭了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,Fds是整数,用于在内核的文件描述表中查找打开的文件.因此,如果您有这样的代码段:

As far I understand it, Fds are integers that are used to look up the open files in the kernel's file description table. So therefore if you have a code segment like this:

int fd[2], temp1, temp2;
pipe(fd);
temp1 = fd[0];
temp2 = fd[1];
close(temp1);
close(temp2);

管道的所有文件描述符都已关闭,因此管道将不再存在.由于FD只是整数,所以说close(temp1)与说close(fd[0])是相同的.

All the file descriptors to the pipe are closed and thus the pipe would no longer exist. Since FDs are simply ints, saying close(temp1) is identical to saying close(fd[0]).

鉴于所有这些情况(如果我有误会,请告诉我),我对fork()调用后发生的事情感到困惑.由于子进程继承了与父进程相同的FD和状态,因此子进程的FD应该与父进程具有相同的整数.因此,按照这种逻辑,如果我在子级中close(fd[0]),我相信这也会阻止父级访问文件.由于close()从文件描述符表中释放"该整数,因此父级不应有任何引用文件的方法.

In light of all this (and please let me know if I am misunderstanding) I am confused by what happens after a fork() call. Since the child process inherits the same FDs and state of the parent, the child's FDs should be the same ints as the parents. So by that logic, if I close(fd[0]) in the child, I believe it would also prevent the parent from accessing the file. Since close() "frees" that integer from the file descriptor table, the parent should not have any way to reference the file.

是这种情况吗?这似乎不太可能是实际情况,因为这将导致父母和孩子之间的FD很难使用(特别是因为您不知道哪个进程将首先运行).因此,如果此逻辑不正确,fork()上的FD是否重复?父Fds和子Fds在文件描述符表中的关系如何,尤其是在close()调用之间?能够绘制文件描述符表对我有很大帮助,所以我想尽可能具体地回答.

Is this the case? It seems very unlikely that this is the actual case, since it would cause FDs between parents and children to be very difficult to use (especially since you do not know which process will run first). So if this logic is incorrect, are the FDs duplicated on fork()? How in the file descriptor table are the parent and child Fds related, especially between close() calls? It helps me a lot to be able to draw out the file descriptor table, so I would like as specific as an answer as possible.

谢谢您的帮助!

推荐答案

否.子级关闭文件的方式只会影响子级文件描述符的副本,而不会影响父级文件描述符的副本.

No. What the child does in the way of closing files affects just the child's copy of the file descriptors, without affecting the parent's copy of the file descriptors.

但是,在分叉之后,两组文件描述符(在父项和子项中)立即引用同一组打开的文件描述(请注意描述符"与描述"术语).如果孩子进行诸如读或查找之类的事情会影响文件描述,那么孩子的活动也会影响父母.

However, immediately after forking, both sets of file descriptors (in parent and child) refer to the same set of open file descriptions (note the 'descriptor' vs 'description' terminology). If the child does things such as read or seek that affect the file description, then the child's activities affect the parent too.

您将要研究 open() fork()

You'll want to study the POSIX specifications of open(), fork() and execve() (especially the execve() page) with considerable care.

这篇关于在C语言中,子级关闭的文件描述符在父级中也关闭了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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