C program to demonstrate fork() and pipe()

In this problem, we will demonstrate fork() and pipe(). Here we will create a C program for Linux that will concatenate two strings, using 2 processes − one will take input and send it to another which will concatenate the string with a predefined string and return the concatenated string.

Note: This program requires a Unix-like system (Linux/macOS) with support for fork() and pipe() system calls. It will not compile on Windows unless using WSL or similar environment.

Fork() and Pipe() Overview

fork() − creates a child process. This child process has a new PID and PPID.

pipe() − a Unix/Linux system call used for inter-process communication. It creates a unidirectional data channel.

Syntax

#include <unistd.h>
#include <sys/types.h>

pid_t fork(void);
int pipe(int pipefd[2]);

How It Works

The program creates two processes using fork() with three possible return values −

  • return value < 0: process creation failed
  • return value = 0: child process
  • return value > 0: parent process (returns child's PID)

We create two pipes for bidirectional communication − one from P1 to P2 and another from P2 to P1.

Parent Process (P1) Child Process (P2) pipe p12[2] "Learn programming" pipe p21[2] "Learn programming at tutorialspoint" Input: "Learn programming" + " at tutorialspoint" Final Output: "Learn programming at tutorialspoint"

Example

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>

int main() {
    int p12[2];  // Pipe from parent to child
    int p21[2];  // Pipe from child to parent
    char fixed_str[] = " at tutorialspoint";
    char input_str[100];
    pid_t P;
    
    // Create two pipes
    if (pipe(p12) == -1 || pipe(p21) == -1) {
        fprintf(stderr, "Failed to create pipe
"); return 1; } printf("Enter string: "); scanf("%99s", input_str); // Prevent buffer overflow P = fork(); if (P < 0) { fprintf(stderr, "fork Failed
"); return 1; } else if (P > 0) { // Parent process char concat_str[200]; close(p12[0]); // Close read end of p12 write(p12[1], input_str, strlen(input_str) + 1); close(p12[1]); // Close write end of p12 wait(NULL); // Wait for child to complete close(p21[1]); // Close write end of p21 read(p21[0], concat_str, sizeof(concat_str)); printf("Concatenated string: %s
", concat_str); close(p21[0]); // Close read end of p21 } else { // Child process close(p12[1]); // Close write end of p12 close(p21[0]); // Close read end of p21 char concat_str[200]; read(p12[0], concat_str, sizeof(concat_str)); // Concatenate strings strcat(concat_str, fixed_str); close(p12[0]); // Close read end of p12 write(p21[1], concat_str, strlen(concat_str) + 1); close(p21[1]); // Close write end of p21 exit(0); } return 0; }

Sample Input and Output

Enter string: Learn
Concatenated string: Learn at tutorialspoint

Key Points

  • Two pipes are needed for bidirectional communication between processes
  • Always close unused pipe ends to avoid blocking
  • Use wait(NULL) to synchronize parent and child processes
  • Check return values of system calls for error handling

Conclusion

This program demonstrates inter-process communication using fork() and pipe() system calls. The parent process sends input to the child, which concatenates it with a predefined string and returns the result through pipes.

Updated on: 2026-03-15T12:51:09+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements