avatarDennis O'Keeffe

Summary

This context discusses the use of pipes and named pipes in Unix systems, specifically focusing on how they can be used to connect programs and pass data between them, with an example implementation in Golang.

Abstract

The context begins by explaining the concept of pipes in Unix systems and how they can be used to pass data between programs using the | operator. It then introduces the concept of named pipes, which are an extension of traditional pipes that allow for more complex inter-process communication. The author explains how to create named pipes using the mkfifo command and demonstrates how to use them to pass data between programs. The context then shows how to use named pipes in a Golang program to read data from a named pipe and output it to the console. The author concludes by discussing the potential uses of named pipes in software development and provides links to additional resources for further reading.

Bullet points

  • Pipes are a way to pass data between programs in Unix systems.
  • Named pipes are an extension of traditional pipes that allow for more complex inter-process communication.
  • Named pipes can be created using the mkfifo command.
  • Named pipes can be used to pass data between programs in a more flexible way than traditional pipes.
  • Golang programs can read data from named pipes and output it to the console.
  • Named pipes have potential uses in software development, such as in logging and monitoring.
  • Additional resources are provided for further reading on pipes and named pipes in Unix systems and Golang.

In Unix systems, pipes and redirections are the bridges that join our programs. They are an underrated resource that can aid developers in their own work in powerful ways.

Today’s post will explore pipes from their humble beginnings to how we use them in our programs (with example help from Golang).

Humble beginnings

Let’s start with the humble pipe operator |. If you are running a Unix shell, we can explore the pipe operator with some simple examples:

In this short example, we are echoing “Hello friends” and using a pipe to pass that output to the translate characters program tr and taking all the lower case letters and upper casing them!

If you are unfamiliar with tr, run man tr to see more. The description includes information on [:class:] usage.

In fact, we could add more pipes if we wanted to “glue” together more programs. We could even just do that with passing out put back to translate again!

You get the gist. What gets cool though is that you can create “named pipes” to start piping in some cooler ways.

Named pipes

What are “named pipes?”. Let’s define this with thanks again to our best friend Wikipedia:

In computing, a named pipe (also known as a FIFO for its behaviour) is an extension to the traditional pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication (IPC).

For clarification, FIFO in this context stands for “first-in, first-out”. We now also understand that it is an extension on tradition pipes, so how do we make one? Luckily, we have a program mkfifo.

In terminal, we can run this and se that a named pipe will be created in the current working directory.

If we now to redirect anything to this named pipe ie ls > example-pipe, the program will wait for something for the named pipe to pass to.

In another terminal, we can do that very things by running cat < example-pipe to see the output in that terminal. Neat!

Running the output of a named pipe to Golang

Now, let’s see how this can be applied to our programs. Here, I am going to use a Golang program to demonstrate how we can do this. I won’t dive too deep into the code (Go has the amazing go doc <search> feature to see information from the terminal) as this is simply a demo.

In the current working directory, create a main.go file and add the following:

Our main function calls two functions:

  1. SetupCloseHandler that creates a channel to listen out for our POSIX standard interrupt signal to exit the program.
  2. ReadPipe that creates an infinite loop in the program to read from the "example-pipe" every 100ms.

Author’s note: ReadPipe and SetupCloseHandler did not need to be public functions, I am just copying them across from other packages in my program that did require it to be so.

If we run go run main.go in one terminal and echo "test" > example-pipe into another, you can see that we get the output through the pipe in our Go program!

Since we are indefinitely checking the pipe until the terminate signal is passed, we can run whatever we want into that example-pipe and see it coming out through our program.

Of course, once we run our terminate signal, we can close the Go program (and you can see Go handling that signal).

Awesome! We’ve managed to create some named pipes and use content piped into them in our program!

Conclusion

This has been a short look at pipes and tying them into our programs. These become very useful when you are building tools to glue others together.

For example at work, we have a tool that I am about to take ownership of which connects together our developer experience from running our different services to migrations, installations and more. Named pipes is our way of being able to selectively pipe logs from each of those running services into one place to empower the developer experience and ensure that they are not overwhelmed!

I have linked a bunch of great resources below that relate to more information on both the pipes and everything that has been happening on the Golang side of things.

Resources and further reading

  1. Bufio scanner
  2. Pipes, redirection and stdout/stdin
  3. UNIX named pipes
  4. Golang Pipe test
  5. Golang Pipes
  6. Golang Mutexes
  7. Error handling in Go
  8. Named Pipe — Wikipedia
  9. Wait Group — Golang
  10. Piping in Unix or Linux

Image credit: Timothy L Brock

Originally posted on my blog.

Unix
Golang
Recommended from ReadMedium