Fork

Description of fork.

Fork/Processes/Pipes

Unix Pipes

The parent and the child are separate processes. The fork launches the child and the parent and child run independently and concurrently. The child is an exact copy of the parent. The child has a copy of all of the variables at the time of the fork. Therefore, at the time of the fork, the child "knows" everything the parent "knows", but then they are on their separate ways. Changes made to variables in the parent are made to the parent's variables only and unknown to the child and visa versa. In order for the parent and child to "share" information or "communicate", other mechanisms must be used.

One mechanism that the parent and child can use to communicate is called a pipe. Just like a real physical pipe, information flows from one end to the other. Information is bidirectional. A process writes to one end of the pipe to send information and reads the other end of the pipe to receive information.

The pipe is used to copy information from the address space of one process to the address space of another process, thus allowing communication between the two processes. The Linux kernel represents the pipe as a file descriptor.

The pipe write operation is asynchronous. The read operation is blocking.

To create a pipe, a process makes the following call to the Linux Kernel:

pipe(pipeIdentifier);

A pipe created prior to the execution of the fork is known by both the parent and the child. They can, therefore, use it to communicate.

A FIFO data structure is created in the kernel. The kernel represents the pipe as a file descriptor. The file identifier pipeIdentifier[0] is a file pointer to read the end of the pipe. The file identifier pipeIdentifier[1] is a file pointer to write the end of the pipe.

If a process does not intend to use a pipe end (e.g. only read a pipe, not write it), it should explicitly close that end so that an end of file condition can be detected. Because the representation is a file descriptor, the close function is called to close the end of a pipe. Example process does not intend to read:

close (pipeIdentification[0]);

Sample code in which after a fork, the parent sends data to a child via the pipe. The child receives data from the parent and prints it.

Maintained by  Barbara Bracken
Last Modified 11/07/2025
This page is copyright © by Barbara Bracken