closing the unwanted file descriptors

5,561

A fork is really a fork. You obtain two almost identical processes. The main difference is the returned value of the fork() system call which is the pid of the child in the one that is identified as parent and 0 in the child (which is how the software can determine which process is considered the parent (the parent has the responsibility to take care of its children) and which is the child).

In particular, the memory is duplicated, so the fd array will contain the same thing (if fd[0] is 3 in one process, it will be 3 as well in the other) and the file descriptors are duplicated. fd 3 in the child will point to the same open file description as fd 3 in the parent.

So fd 3 of both parent and child will point to one end of the pipe, and fd 4 (fd[1]) of both parent and child will point to the other end.

You want to use that pipe for one process to send data to the other one. Typically one of the processes will write to fd 4 and the other one will read from fd 3 until it sees end of file.

end of file is reached when all the fds open to the other side of the pipe have been closed. So if the reader doesn't close its fd to the other side, it will never see the end of file.

Similarly if the reader dies, the writer will never know that it must stop writing if it hasn't closed its own fd to the other side of the pipe.

Share:
5,561

Related videos on Youtube

abkds
Author by

abkds

Updated on September 18, 2022

Comments

  • abkds
    abkds over 1 year

    I am trying to understand how pipes work . This code snippet is from some website .

    So my understanding of the program goes like this :

    When we do pipe(fd) then fd[0]=3 and fd[0]=4 ( assuming only 0,1 & 2 are open till now ) . Now we fork the parent and then if a child is not created then we have error and it exits .

    But if it child is created successfully , are the file descriptors copied to the child , I mean is fd[0]=3 and fd[1]=4 for the child process also ? fd[0] and fd[1] are file handles to which files ( we have not specifically specified ) ?

    Let us suppose that we want child to read from parent then , parent should close fd[0] and child should close fd[1] but why ? What will happen if we do not close them ? And I didn't understand On a technical note, the EOF will never be returned if the unnecessary ends of the pipe are not explicitly closed. .

    If the parent wants to receive data from the child, it should close fd1, and the child should close fd0. If the parent wants to send data to the child, it should close fd0, and the child should close fd1. Since descriptors are shared between the parent and child, we should always be sure to close the end of pipe we aren't concerned with. On a technical note, the EOF will never be returned if the unnecessary ends of the pipe are not explicitly closed.

        #include <stdio.h>
        #include <unistd.h>
        #include <sys/types.h>
    
        main()
        {
                int     fd[2];
                pid_t   childpid;
    
                pipe(fd);
    
                if((childpid = fork()) == -1)
                {
                        perror("fork");
                        exit(1);
                }
    
                if(childpid == 0)
                {
                        /* Child process closes up input side of pipe */
                        close(fd[0]);
                }
                else
                {
                        /* Parent process closes up output side of pipe */
                        close(fd[1]);
                }
                .
                .
        }
    
  • abkds
    abkds almost 10 years
    I think I understand most of what you say but which files are opened for fd 3 and fd 4 . Are those two the same files which is internally opened somewhere ( where one writes and other reads ) not shown to the user ?