IPC with named pipe

Linux user would know what is a pipe, and named “|” as the pipe character. Named pipe is actually similar, the difference is, a linux pipe persist as long as the process that uses the pipe last. But a named pipe persist even if the process have closed.

Why you need a named pipe? It is actually a nice way to provide IPC to 2 processes. The coolest thing of all, It is shares file semantics, i.e you can use open() and close()

Here is how you create and use a named pipe

Of course you can replace cat and echo with your own process. One way this is really useful is by opening the pipe in your program.

This is how you write a writer

As you can see it shares semantic with file object, with open(), read(), write(), but with caveat.

  • open() can block if it is called on one side
  • read() can block if close() is not called on the writer

Here you are a simple way to do IPC on linux using file semantic.