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

Named Pipe Example Using Raspberry Pi – (Part 33/38)

By Ajish Alfred October 13, 2013

The Raspberry pi is a device which uses the Broadcom controller chip which is a SoC (System on Chip). This SoC has the ARM11 processor which runs on 700 MHz at its core. The operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board. Linux operating systems especially Ubuntu is preferred for all kind of programming and development. The Raspberrypi is a board actually designed for helping computer education for remote schools but it is a nice platform for programmers especially beginners to explore various coding techniques.

Multi-tasking Operating Systems can run several processes at a time creating and effect of parallel processing with the help of the high speed processor. There are different kinds of Inter Process Communication (IPC) system and the Named Pipe is one of the simplest of them. The Named Pipe is actually a temporary file having a particular name and stored at a particular directory whose name and location are known to the processes which needs to communicate with each other. The Named Pipe is also called First In First Out (FIFO).
This project demonstrates how two programs communicate each other using named pipe. In this project a simple program is written which creates a pipe and continuously checks for any data on the pipe. The data is written to the pipe by simply using terminal commands. As soon data appears on the pipe, the program reads it and prints back on the terminal.

 


 

This project basically requires two programs which are meant to send data in between them, and a named pipe which will be created by anyone of them. The entire system can be represented with the help of the following diagram:

Block Diagram of Name Pipe Process using Raspberry pi

Fig. 2: Block Diagram Of Name Pipe Process Using Raspberry Pi

 

In this project, consider the PROCESS 1 is the process which creates the NAMED PIPE and always trying to read data from the pipe. PROCESS 2 is the process which writes some data to the NAMED PIPE occasionally. The code for the PROCESS 1 is written in C and is compiled and made executable and the PROCESS 2 in this project is the Shell which the user uses to write data to the NAMED PIPE using the Shell commands like ‘echo’.
The functions used in the coding of the PROCESS 1 for creating the NAMED PIPE and for the reading and writing operations are explained in the following section.
 

 

mkfifo ()

 

The function mkfifo() creates a temporary file at the required directory with the required access permissions set on it. The prototype of the function is declared as the following;
 

 

int mkfifo ( const char *pathname, mode_t mode );

 

The first argument is the required pathname of the directory where the named pipe needs to be created. The second argument is the user permission that needs to be set on the file. Using the value 0777 as the second argument allows permission to all users of the system to read from and write into that named pipe. For using this function in the C code, two header files <sys/types.h> and <sys/stat.h> should be included.
mkfifo (“/tmp/my_fifo”, 0777 );
The above function call creates a named pipe called “my_fifo” in the location “/tmp” with access permission to all the users of the system.
 

 

open ()

 

The open () function opens a particular file at a specified path with the required flags set and it returns a file descriptor corresponding to that file to the process which called the open function. Using that file descriptor the process can access the file.
The prototype for the open () function is declared as the following;
 

 

int open ( const char *pathname, int flags );

 

The first argument is the path of the location where the file needs to be opened exists and the second parameter is the flags that need to be set for the file. The flags that set on the file decide the way in which the file can be accessed. 
open ( “/tmp/my_fifo”,  ( O_RDONLY | O_NONBLOCK )  );
The above function call opens the file named “my_fifo” in the location “/tmp” with the flags set to access the file as Read Only. The function call will returns a small positive integer value called file descriptor using which the process can access the file. The header files needed to be included when using this function is <sys/types.h> and <fcntl.h>.
 

 

 read ()

 

The read () function tries to read the data from a file using its file descriptor generated by the open () function while opening it. The arguments of the functions decide the maximum number of bytes that can be read at a time and where to store the read data. It also returns the number of bytes of data successfully read from the file. The prototype of the function is declared in the header file <unistd.h> as given below;
 

 

ssize_t read ( int fd, void *buf, size_t count );

 

The first argument of the function is the file descriptor of the file that need to be read, the second argument is pointer to a data container into which the read data needs to be stored and the third argument is the maximum number of bytes that can be read into the data container.
read ( our_input_fifo_filestream, ( void* ) rx_buffer, 255 );
The above statement can read 255 characters of data at a time and store it in an array rx_buffer, and also returns the number of data read from the file at that particular attempt.
The code for the PROCESS 1 is written into a file say fifo.c and the compiled to an executable file say fifo. It is then executed from the terminal using the command;
 

 

./fifo

 

This will create a temporary file called ‘my_fifo’ in the directory /tmp. The user can write data into the temporary file from the terminal itself using the following command;
 

 

echo abcdef >> /tmp/my_fifo

 

Command to write data in temporary file from terminal in Raspberry pi

Fig. 3: Command To Write Data In Temporary File From Terminal In Raspberry pi

 

Project Source Code

###

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define OUR_INPUT_FIFO_NAME "/tmp/my_fifo"

unsigned char rx_buffer [ 256 ];
int rx_length;
int our_input_fifo_filestream = -1;

int result;

int main ()
{
printf ( "Making FIFO...n" );
result = mkfifo ( OUR_INPUT_FIFO_NAME, 0777 );
our_input_fifo_filestream = open ( OUR_INPUT_FIFO_NAME, ( O_RDONLY | O_NONBLOCK ) );

while ( 1 )
{
rx_length = read ( our_input_fifo_filestream, ( void* ) rx_buffer, 255 ); //Filestream, buffer to store in, number of bytes to read (max)
if ( rx_length > 0 )
{
rx_buffer [ rx_length ] = '';
printf ( "FIFO %i bytes read : %sn", rx_length, rx_buffer );
}else;
}
}
 

###

 


Project Video


Filed Under: Raspberry pi
Tagged With: Raspberry Pi
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.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

  • I/O constraint for Hold check
  • Inverting OpAmp - basic circuit question
  • Why does Synopsys DC need Inverters and Buffers if my RTL only has XOR logic?
  • GanFet power switch starts burning after 20 sec
  • FSM for an UART TX

RSS Electro-Tech-Online.com Discussions

  • stud mount Schottky diodes
  • Hi Guys
  • Precision CAD Drafting Services for Architectural & Engineering Projects
  • LED circuit for 1/6 scale diorama
  • using a RTC in SF basic

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

  • Fischer connector system adds ratchet locking system designed for 300g shock resistance
  • Littelfuse introduces tactile switch with enhanced bracket peg design for mounting strength
  • Infineon releases GaN switch with monolithic bidirectional design
  • Sienna Semiconductor data converters feature sample rates from 20 to 250 Msps
  • Delta’s 5,500 W power supplies achieve 97.5% energy efficiency for AI servers

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