Engineers Garage

  • Electronic Projects & 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 – Create Pipe With C In Linux (Part 12/15)

By Ashish Vara July 20, 2016

How to create pipe in Linux

Overview of Pipes in Linux

Fig. 1: Overview of Pipes in Linux

 

Pipe is one way communication of data between one process to another process. It is a form of communication called Interprocess Communication (IPC). 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. These file descriptors may pass through fork ( ) system call. Fork ( ) creates a new process which shares the pipe with them. It takes no argument and returns the process ID. You can learn more about Fork ( ) system call in Process in Linux tutorial.

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 is half duplex pipe so each process must be closed before using another.

 

Pipe ( ) System Call

Pipe ( ) system call creates a pair of file descriptors for communication between two processes. First file descriptor is associated with read operation and second file descriptor is associated with write operator.

SYNOPSIS of pipe ( ) system call:

            int pipe ( int file_descriptor [2]);

Array file_descriptor is used to store return pair of file descriptor. file_descriptor [0]  refers to the read end of pipe and file_descriptor [1] refers to the write end of pipe.

It returns Zero on success or returns -1 on error and sets the appropriately errno.

Errors may occur as following on return -1:

EFAULT – file_descriptor is not valid.

EMFILE – The per-process limit on the number of open file descriptors has been reached.

ENFILE – The system-wide limit on the total number of open files has been reached.

 

Create simple demo pipe

This is the basic demo tutorial of how to work with pipe. It is an example of only  transferring the data from one write file descriptor to read file descriptor without creating process. Before making the program, refer to the tutorial How to create first C program in Linux.

//*****************************Pipe_demo.c*********************************//

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int main()

{

            int no_of_byte;

            char message[1025];

            char  *write_msg = “Hello this is demo tutorial of working of pipe….”;

            int  file_d[2];                                                   // array of file descriptor which stores pair of file descriptors

      

            pipe(file_d);                                                    // create pipe;

            write(file_d[1] , write_msg , strlen(write_msg)); // write the message to pipe using write file descriptor;

           

            no_of_byte = read(file_d[0] , message , 1024 ); // read the message from pipe using read file deascriptor;

            if(no_of_byte >= 0)                                        // check the number of byte greater than zero;

            {

                        message[no_of_byte] = 0;                   // store last byte of message NULL;

                        printf(“Read %d byte from pipe : %sn ” , no_of_byte , message); // print the  message which is read from pipe and store it in message [] array;

            } 

            else

            {

                        perror(“read”);

            }

            exit(0);                                                 // exit from program and close the pipe;

}

//*****************************Pipe_demo.c*********************************//

Write or copy same code in your Linux text editor and save it as Pipe_demo.c. You can save file with any name  that you wish.

 

Compiling the source code

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

Compile Pipe_demo.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 is executable file of Pipe_demo.c and locates it in the same directory where source file is saved.

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

gcc -o  pipedemo  Pipe_demo.c

Now GCC will generate executable file named pipedemo instead of a.out. Before running the executable file, make sure that the executable file is generated within the same directory where the 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:

./pipedemo

Following result will print on the monitor screen:

Read 50 byte from pipe : Hello this is demo tutorial of working of pipe….


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

  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • What is the purpose of using a VCVS?
  • Checking line impedance
  • Why do I hear whistling sounds in my vents?
  • Mains inverter with switching node going out on the mains cable!?

RSS Electro-Tech-Online.com Discussions

  • Why can't I breadboard this oscillator?
  • Failure of polypropylene motor-run capacitors
  • a point I can't understand about the sinc expression
  • Quick advice on remote car starter?
  • Wideband matching an electrically short bowtie antenna; 50 ohm, 434 MHz

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection
  • Convection-cooled power controller integrates EtherCat connectivity

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

  • Electronic Projects & 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