After fork, the parent and child processes share the file descriptor created with pipe?

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 does not pass variables with its parent process. Then, why the parent can write to one file descriptor, and the child process can get data by reading from another file descriptor. Is it because they are somehow controlled by the function of the pipe?

+7
source share
3 answers

File descriptors, including pipes, are duplicated on the fork - the child process ends with the same file descriptor table, including stdin / out / err and pipe, since the parent element was immediately before the fork.

Based on my understanding, the child process created by fork does not pass variables with its parent process.

This is not entirely true: variables are not passed on to variable parents, but the values ​​that the parent had immediately before the fork are now visible to the descendant.

In any case, pipes exist inside the operating system, not inside the process. Thus, data recorded at one end of the pipe becomes visible to any other process containing FD for the other end. (If several processes try to read data, the first process to try to get read() data gets it and any other processes skip.)

+14
source

Variables are not shared, for example. if you write file_pipes[0] = 999 in the child, this will not be reflected in the parent. The file descriptors are separated (the FD x number in the child language is the same as the FD x number in the parent element). That is why (for example) you can redirect the output of a shell script that executes other commands (since they have the same standard output file descriptor).

+5
source

You are right - ordinary variables are not shared between parent and child.

However, pipes are not variable. This is a pseudo file specifically designed to connect two independent processes. When you write to the handset, you do not change the variable in the current process - you send data to the operating system and ask it to make this data available for the next process to read from the channel.

This is similar to the fact that when you write a real file to disk, except that data is not written to disk, it is simply accessible at the other end of the channel.

+1
source

All Articles