Pipe linux

Pipe linux

Introduction

For this mounth the article will be more technique than usual .
I try to explain you an linux concept that for me is under exploited in our linux application. So i write this post to hilight this concept that i will use in a work project.
this concept is the pipe linux

The pipe concept

If you are a linux user you have certainly cross the path of the pipe command |. But did you know that there is two different type of pipe. And more over that did you know that you can use pipe as a connection protocol. I didn't until i need to create a new application based on module and find this way of communicate between process.

So i will share this discover with you. So there are to type of pipe

uname pipe

The uname pipe is that you can find and use in every day commande for exemple one i regularely used:

ls -ltr | grep .md

This two command will list all the Markdown file in a repository.

To do so ls -ltr and grep will be execute as separet processes and the uname pipe allows them to communicate.

what's append is that the ls write to the pipe and send an end of stream marker when finish to write and the grep is the reader. After the reading the unmame pipe will be automaticly delete.

An unnamed pipe has no backing file: the system maintains an in-memory buffer to transfer bytes from the writer to the reader. Once the writer and reader terminate,
the buffer is reclaimed, so the unnamed pipe goes away.

name pipe

The name pipe will fallow the same principe as an uname pipe except that he as a backing file and a distinct API.
To create a name pipe use the command

mkfifo myPipe

Multiple processes can access this special file for reading and writing like any ordinary file.

The name pipe can provides unidirectional communication.

The name pipe have strict FIFO behavior: the first
byte written is the first byte read, the second
byte written is the second byte read, and so
forth.

to close the pipe

unlink(myPipe)

if you want an example of two way pipe communication you can find a very good article here :

https://levelup.gitconnected.com/inter-process-communication-between-node-js-and-python-2e9c4fda928d

or this video is also very helpfull

Conclusion

Using an anonymous pipe instead of a named pipe depends on the characteristics we’re looking for. Some of them can be persistence, two-way communication, having a filename, creating a filter, and restricting access permissions among others.

Link

https://www.baeldung.com/linux/anonymous-named-pipes#:~:text=A%20FIFO%2C%20also%20known%20as,a%20name%20in%20the%20filesystem.

https://opensource.com/sites/default/files/gated-content/inter-process_communication_in_linux.pdf

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *