How to Control Hardware using Named Pipe

In a Linux operating system all each hardware device is represented as a file. The device can be controlled by simply reading and writing into that file. The hardware of an operating system is on the one side and the user trying to access the hardware is on the other side, and in between them there might be several layers of applications running which communicates each other using inter process communication methods. This project demonstrates how to control a process which can turn on and off the LEDs of the Raspberrypi with the help of another process by writing into a pipe file.

How to Control Hardware using Named PipeThe Raspberrypi is a mini computer which can be used to learn and experiment the concept and working of an Operating System. 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.

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.

In the above figure LED_CONTROL_1 is the process which creates the NAMED PIPE and always trying to read data from the pipe. The process USER_INPUT is the process which writes some data to the NAMED PIPE according to the user commands. The code for the LED_CONTROL_1 is written in C and is compiled and made executable and the USER_INPUT 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.

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;

How to Control Hardware using Named Pipe Schematic

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.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top