Named Pipe Example Using Raspberry Pi




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).
Named Pipe Example Using Raspberry PiThis 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:
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.

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

Named Pipe Example Using Raspberry Pi Schematic#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 ] = ‘\0’;
printf ( “FIFO %i bytes read : %s\n”, rx_length, rx_buffer );}else;}}

 

For more detail: Named Pipe Example Using Raspberry Pi




© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top