Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

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

By Ajish Alfred

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
 

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.

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!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Renesas to demonstrate first working silicon based on the Arm Cortex-M85 processor
  • STMicroelectronics releases first automotive IMU with embedded machine learning
  • Infineon offers DC-DC controller for full LED headlamps without a microcontroller
  • Vishay launches new high-precision, thin-film wraparound chip resistor
  • STMicroelectronics’ common-mode filters ensure signal integrity in serial interfaces

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • What is the function of the long stub?
  • Circuit Performance Exploration
  • Resistor across crystal for biasing the internal op-amp
  • Help with Verilog replicate operator
  • Some question on simple FM transmitter

RSS Electro-Tech-Online.com Discussions

  • Audio equalizer
  • Background of Members Here
  • software PWM
  • SPI Questions
  • Dog Scarer
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 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 | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key 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
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering