After fork, do the parent and child process share the file descriptor created by pipe?

17,073

Solution 1

The variables are not shared e.g. if you write file_pipes[0] = 999 in the child, it will not be reflected in the parent. The file descriptors are shared (FD number x in the child refers to the same thing as FD number x in the parent). This is why (for example) you can redirect the output of a shell script which executes other commands (because they share the same standard output file descriptor).

Solution 2

You're right - ordinary variables aren't shared between the parent and the child.

However, pipes are not variables. They're a pseudo-file specifically designed to connect two independent processes together. When you write to a pipe, you're not changing a variable in the current process - you're sending data off to the operating system and asking it to make that data available to the next process to read from the pipe.

It's just like when you write to a real, on-disk file - except that the data isn't written to disk, it's just made available at the other end of the pipe.

Share:
17,073

Related videos on Youtube

q0987
Author by

q0987

Updated on July 11, 2020

Comments

  • q0987
    q0987 almost 4 years
    int main()
    {
        int data_processed;
        int file_pipes[2];
        const char some_data[] = "123";
        char buffer[BUFSIZ + 1];
        pid_t fork_result;
    
        memset(buffer, '\0', sizeof(buffer));
    
        if (pipe(file_pipes) == 0) {
            fork_result = fork();
            if (fork_result == -1) {
                fprintf(stderr, "Fork failure");
                exit(EXIT_FAILURE);
            }
    
    // We've made sure the fork worked, so if fork_result equals zero, we're in the child process.
    
            if (fork_result == 0) {
                data_processed = read(file_pipes[0], buffer, BUFSIZ);
                printf("Read %d bytes: %s\n", data_processed, buffer);
                exit(EXIT_SUCCESS);
            }
    
    // Otherwise, we must be the parent process.
    
            else {
                data_processed = write(file_pipes[1], some_data,
                                       strlen(some_data));
                printf("Wrote %d bytes\n", data_processed);
            }
        }
        exit(EXIT_SUCCESS);
    }
    

    Based on my understanding, the child process created by fork doesn't share variables with its parent process. Then, why here the parent can write to one file descriptor and child process can get the data by reading from another file descriptor. Is this because they are controled somehow by the pipe function internally?

Related