Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  64] [ 0]  / answers: 1 / hits: 89408  / 2 Years ago, thu, september 1, 2022, 9:59:40

I recently read about named pipes, and I couldn't understand why they exist.

I've read somewhere that using a named pipe is less time-consuming than using a file.



Why is this so?

The named pipes also have to be stored in memory (and maybe swapped, just like files).

As far as I can see, they must get an inode which must be referenced by the current directory, just like files.
Also, they must be removed by the programmer, just like files.



So where does the advantage lie?


More From » files

 Answers
0

Almost everything in Linux can be considered a file, but the main difference between a regular file and a named pipe is that a named pipe is a special instance of a file that has no contents on the filesystem.


Here is quote from man fifo:



A FIFO special file (a named pipe) is similar to a pipe, except that it is accessed as part of the filesystem. It can be opened by multiple processes for reading or writing. When processes are exchanging data via the FIFO, the kernel passes all data internally without writing it to the filesystem. Thus, the FIFO special file has no contents on the filesystem; the filesystem entry merely serves as a reference point so that processes can access the pipe using a name in the filesystem.


The kernel maintains exactly one pipe object for each FIFO special file that is opened by at least one process. The FIFO must be opened on both ends (reading and writing) before data can be passed. Normally, opening the FIFO blocks until the other end is opened also.



So actually a named pipe does nothing until some process reads and writes to it. It does not take any space on the hard disk (except a little bit of meta information), it does not use the CPU.


You can check it by doing this:


Create a named pipe


$ mkfifo /tmp/testpipe

Go to some directory, for example /home/user/Documents, and gzip everything inside it, using named pipe.


$ cd /home/user/Documents
$ tar cvf - . | gzip > /tmp/testpipe &
[1] 28584

Here you should see the PID of the gzip process. In our example it was 28584.


Now check what this PID is doing


$ ps u -P 28584
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
c0rp 28584 0.0 0.0 29276 7800 pts/8 S 00:08 0:00 bash

You will see that it is using no resources. 0% CPU usage, 0% memory usage.


Verify hunch regarding file space usage


$ du -h /tmp/testpipe
0 testpipe

And again 0, nothing. The testpipe could be used again if needed.


Don't forget to kill gzip, using kill -15 28584. And remove our named pipe using rm /tmp/testpipe


Example Usages


You can redirect almost everything using named pipe. As example you can see this one line proxy.


Also here is one more nice explanation of named pipe usage. You can configure two processes on one server to communicate using a named pipe instead of TCP/IP stack. It is much faster, and does not load network resources. For example your Web Server can communicate with the database directly using a named pipe, instead of using localhost address or listening to some port.


[#25979] Friday, September 2, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
assionortly

Total Points: 423
Total Questions: 121
Total Answers: 115

Location: Chad
Member since Wed, Sep 30, 2020
4 Years ago
;