Blog

Use and examples of Named Pipes

Tags: Linux

Published on: April 6, 2019 by Anitta Jose

Use and examples of Named Pipes

Scenario:

Pipe is a method for Inter Process Communication (IPC) which send output of one process to another process. As you know, pipes are of two types:

  • Unnamed Pipes

  • Named Pipes

Unnamed Pipe

An example of unnamed pipe is as follows:

$ ls | wc -l

Here output of first command is given as an input to the second command. On the command line, it is represented by a “|” symbol between two commands. This is a one way pipe which usually transfers data between parent and child process. This pipe vanishes when either of the process completes its execution or when they are closed. It is used for local communication and cannot be used over a network.

Named Pipe

The Named pipe is a method for passing information from one computer process to another using a pipe which is given a specific name. Unix and Windows, both have “Named pipes”, but they behave differently. On Unix, a named pipe is one-way street which typically has just one reader and one writer – the writer writes, and the reader reads, you get it? On Windows, the “Named pipe” is an IPC object more like a TCP socket – things can flow both ways and there is some metadata (You can obtain the credentials of the thing on the other end etc).

Named Pipe is also called FIFO, which stands for First In First Out. On older Linux system, named pipes are created using the command mknod whereas mkfifo is used in modern systems.

$ mkfifo pipe1 $ ls -l pipe1 prw-rw-r-- 1 user user 0 Dec 13 10:12 pipe1

Here, ‘p’ indicated that ‘pipe1’ is a pipe. Once created, you can use the pipe just like a normal file (open, close, write, read, etc).

The main difference between a regular file and a named pipe is that a named pipe is a special type of file which has no contents, but accessed as a part of the filesystem. It can be opened by multiple process for reading and writing. A named pipe is opened on both ends for reading at one and writing at another. It does not use CPU too. Consider the following example,

$ mkfifo /tmp/myfile.sock $ cd /home/user/documents $ t&r cvf - . | gzip > /tmp/myfile.sock & [2958]

Here, you should see the PID of the gzip process. In our example it is 2958. Now let’s check what this PID is doing using ps acommand.

$ ps u -P 2958 USER  PID  %CPU %MEM  VSZ   RSS   TTY   
STAT START TIME COMMAND user 2958  0.0   0.0  39276 7900 pts/4   S  00f08 0f00  bash

You will see that it is using no resources i.e, it has 0% CPU usage and  0% memory usage. Now lets verify the hunch regarding its file space usage:

$ du -h /tmp/myfile.sock 0 myfile.sock

And again 0, nothing. The myfile.sock could be used again if needed. Don’t forget to kill gzip using kill command and remove our named pipe using rm command:

$ kill -15 2958 $ rm /tmp/myfile.sock

Category : Linux

Anitta Jose

Anitta Jose

Anitta is systems engineer since 2015 and holds broad experience in Linux, WordPress, and cPanel systems administration. Her interest lies more in Cloud technologies (AWS). From 2016, she writes blogs to share her experiences with wider audience.

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Categories

Your Cart

Cart is empty.

Subtotal
₹0.00
APPLY
0
Send this to a friend