Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Introduction To Linux-Communication Between Processes Through Pipe (Part 13/24)

By Ashish Vara July 20, 2016

How To Communicate Between Processes Through Pipe

Pipe is a one way communication of data between one process to another process. In the form of Interprocess communication (IPC), it is one type of communication. Refer to the tutorial Pipe in Linux before learning this one.

Process creates a new pipe using pipe ( ) system call. It creates a pair of file descriptor: one for read channel and one for write channel. It can read from pipe by read ( ) system call with first file descriptor and can write to pipe by write ( ) system call with second file descriptor. It has half duplex pipes so each process must be closed before using another.

You can get more information of pipe ( ) system call in How to create pipe in Linux tutorial.

Overview of Communication between Linux Processes

Fig. 1: Overview of Communication between Linux Processes

Create Simple Demo Pipe

This is the basic demo tutorial of communication between two processes through pipe. Pipe is the flow of data communication between two processes. It is an example where the parent process reads data from pipe which is input by child process. Before making program refer to the tutorial How to create first C program in Linux.

Pipe ( ) system call creates a pair of file descriptors. First file descriptor reads from pipe and second file descriptor writes in to pipe. These file descriptor may pass through fork ( ) system call.

fork ( ) system call creates new process from current running process. fork ( ) system call returns twice: once from child process and another time from parent process. It  returns zero from child process and returns value with child PID from parent process. Fork ( ) creates a new process which shares the pipe with them. It takes no argument and returns process ID. You can learn more about Fork ( ) system call in Process in Linux tutorial.

 

//*****************************Pipe_demo1.c*********************************//

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <errno.h>

#define Max_length  1025

 

int main()

{

            char WriteMsg[Max_length];

            char ReadMsg[Max_length];

            int file_d[2];

            int result_pipe;

            pid_t child_t;

            result_pipe = pipe(file_d);

            if(result_pipe < 0)

            {

                        perror(“pipe”);

            }  

            child_t = fork();

            if(child_t >= 0)

            {

                        if(child_t == 0)

                        {

                                    memset (WriteMsg , 0 , sizeof(WriteMsg));

                                    printf (“Enter the Message : “);

                                    fgets (WriteMsg , sizeof(WriteMsg) , stdin);

                                    write (file_d[1] , WriteMsg , strlen(WriteMsg));

                                    exit(0);

                        }

                        else

                        {

                                    memset (ReadMsg , 0 , sizeof(ReadMsg));

                                    read (file_d[0] , ReadMsg , sizeof(ReadMsg));

                                    printf(“Entered message : %sn ” , ReadMsg);

                                    exit(0);

                        }

            }

            else

            {

                        perror(“fork”);

                        exit(2);

            }         

            return 0;

}

//*****************************Pipe_demo1.c*********************************//

Write or copy same code in your Linux text editor and save it as Pipe_demo1.c. You can save the file with any name  of your choice.

memset ( ) is C library function which sets the first number of bytes of the block of memory pointed by array to the specified value. Here memset ( ) sets the WriteMsg and ReadMsg array with zero (NULL) value. 

Declaration of memset ( ) function:

     void *memset (void *str, int value, size_t byte);

str is pointer to the block of memory(i.e. WriteMsg [ ] or ReadMsg[ ]).

value is int value which fills allocated block of memory

byte is first number of byte which we want to set with specific byte.

fgets ( ) takes the data from input terminal and stores it in particular block of memory. In this tutorial, we take input data from input terminal and store it in WriteMsg array and then write into pipe through write ( ) function.

First pipe is created by pipe ( ) system call and it  creates a pair of file descriptors. Child process is created by fork ( ) system call and if it’s successful, we can write data into pipe. After  doing so, stop the child process and go back to the parent process which returns ID of child process. Parent process can read the message from pipe and display it on terminal screen.
 

Compiling The Source Code

After  writing and checking it for error, next step is ready for compiler.

Compile Pipe_demo1.c with GCC by following line:

gcc Pipe_demo.c

After compilation, GCC will generate executable (runnable) file. Here we  haven’t given the name of executable file so compiler automatically gives the a.out name to file. a.out which is an executable file of Pipe_demo1.c and is located in the same directory where source file is saved.

But a good programmer always gives name of executable file using following line instead of upper method:

gcc -o  pipedemo1  Pipe_demo1.c 

Now GCC will generate executable file named pipedemo1 instead of a.out. Before running the executable file, make sure that the executable file is generated within same directory where source file is located.
 

Running The Executable File

Type the file name followed by dot and a forward slash and press the ENTER key as following:

./pipedemo1

Following result will  print on monitor screen:

Enter the Message : type your input message and press enter key

Entered message :  output message


Filed Under: Tutorials

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • How can I get the frequency please help!
  • differential amplifier with active load
  • Battery sensing circuitry for coin cell application
  • I/O constraint for Hold check
  • Question LCD LED IPS display

RSS Electro-Tech-Online.com Discussions

  • 100uF bypass Caps?
  • how to work on pcbs that are thick
  • Fuel Auto Shutoff
  • Actin group needed for effective PCB software tutorials
  • compatible eth ports for laptop

Featured – Designing of Audio Amplifiers part 9 series

  • Basics of Audio Amplifier – 1/9
  • Designing 250 Milli Watt Audio Power Amplifier – 2/9
  • Designing 1 Watt Audio Power Amplifier – 3/9
  • Designing a Bass Boost Amplifier – 4/9
  • Designing a 6 Watt Car Audio Amplifier – 5/9
  • Design a low power amplifier for headphones- 6/9

Recent Articles

  • ITG Electronics releases gate drive transformers with 200 – 450 V DC capability
  • Stackpole introduces HCJ jumpers with 70.7 amp continuous current capability
  • Infineon releases MCU with 128K flash and multi-sense capabilities
  • ST introduces 600V GaN gate drivers with 300 ns start-up time
  • ABLIC releases S-19116 automotive voltage detector with 6.8μs response time

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe