Javatpoint Logo
Javatpoint Logo

C Program to Demonstrate fork() and pipe()

In the following tutorial, we will understand the implementation of fork() and pipe() in C Programming language.

So, let's get started.


  • The fork() function is used to start a new process. This kid process is an exact replica of the parent process. On Unix-like operating systems, it is the main technique for creating new processes.
  • A new process known as a "child process" is created with the fork system function and runs simultaneously with the process that invoked fork() (parent process). Both processes will carry out the next instruction after the fork() system call once a new child process has been started. The same CPU registers, program counter, and open files that the parent process utilizes are used by the child process.
  • It returns an integer value and requires no arguments. Various values returned by fork are listed below ().


  • A pipe can be used so that one process writes to it and another process reads from it. This is known as one-way communication. It begins by opening a pipe, a portion of main memory that functions as a "virtual file."
  • The producing process, along with all of its offspring, may read from and write on the pipe. This "virtual file" or pipe can be written to by one process, and read from by another process that is connected to it.
  • A process is halted until anything is written to the pipe if it tries to read before anything has been written.
  • The read and write endpoints of the pipe are given the first two open places that the pipe system call discovers in the process' open file table.

C Program to Demonstrate fork() and pipe():

Create two processes, P1 and P2, using a C program. P1 hands a string to P2 after receiving it. Without utilizing a string function, P2 joins the received string to another string and sends the result back to P1 for printing.



We utilize a fork to generate a child process (). Fork () returns the following information: 0 fail to create child (new) process =0 for child process >0, i.e. the process ID of the child process to the parent process. Parent process will run when >0.

Information may be sent from one process to another using the pipe() function. Since pipe() is unidirectional, two pipes-one for each direction-can be set up to provide two-way communication between processes.

Inside Parent Process: We first close the first pipe's reading end (fd1[0]), then we write the string via the pipe's writing end (fd1[1]). Parent will now wait till the child's process is complete. After the child process, the parent will shut the second pipe's writing end (fd2[1]), and read the string through the pipe's reading end (fd2[0]).

Within the child process, the child reads the first string that the parent process delivered by shutting the writing end of the pipe (fd1[1]), concatenates the two strings after reading them, and then sends the string to the parent process via the fd2 pipe before exiting.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA