As far as I understand, Fds are integers that are used to search for open files in the kernel file description table. 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);
All file descriptors in the pipe are closed and therefore the pipe will no longer exist. Because FDs are just ints, saying that close(temp1) identical to the expression close(fd[0]) .
In light of all this (and please let me know if I don't understand). I am confused about what happens after the fork() call. Since the child process inherits the same FD and parent state, the child FDs must be the same ints as the parents. Therefore, by this logic, if I close(fd[0]) in a child, I believe that this will also prevent the parent from accessing the file. Since close() "releases" this integer from the file descriptor table, the parent must not reference the file.
This is true? It seems very unlikely that this is a factual case, as this can lead to the fact that FDs between parents and children will be very difficult to use (especially since you do not know which process will work in the first place). So, if this logic is wrong, are FD duplicated on fork() ? How are parent and child Fds related in the file descriptor table, especially between close() calls? It helps me a lot to draw a file descriptor table, so I would like to answer as specific as possible.
Thanks for any help with this!
source share