DEV Community

BC
BC

Posted on

12

Use `mkfifo` to create named pipe - Linux Tips

We can use mkfifo or mknod command to create a named pipe. A pipe is a structure which one end can send message and the other can consume it.

To create a named pipe, we can use mkfifo or mknod:

$ mkfifo pipe1

# we can see the file type is "named pipe" using `file` command
$ file pipe1
pipe1: fifo (named pipe)

$ ls -al pipe1
prw-r--r-- 1 userx userx  0 Dec 29 20:35 pipe1

# or we can use mknod, "p" indicate this "node" is a pipe
$ mknod pipe2 p
$ file pipe2
pipe2: fifo (named pipe)
Enter fullscreen mode Exit fullscreen mode

Now let's send/consume messages with this pipe. Open a terminal window:

$ tail -f pipe1
Enter fullscreen mode Exit fullscreen mode

Open another terminal window, write a message to this pipe:

$ echo "hello" >> pipe1
Enter fullscreen mode Exit fullscreen mode

Now in the first window you can see the "hello" printed out:

$ tail -f pipe1
hello

Enter fullscreen mode Exit fullscreen mode

Because it is a pipe and message has been consumed, if we check the file size, you can see it is still 0:

$ ls -al pipe1
prw-r--r-- 1 userx userx 0 Dec 29 20:35 pipe1
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Here is a quick video demo on how to do that as well:

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay