Skip to content
A cross-platform library to use named pipe
Latest commit fd20588 Oct 15, 2015 @shurizzle Dump version
Failed to load latest commit information.
Rakefile Cleaned up the code a bit and added a test suite. May 13, 2012

ruby-fifo Build Status

A small, simple library for using Fifos in Ruby. A Fifo is traditionally a Unix idea that lets processes communicate by writing to and reading from a special kind of file in the filesystem. More information on fifos can be found here:


To install ruby-fifo, execute the following command at your terminal:

$ gem install ruby-fifo

Being sure not to include the dollar sign. The dollar sign is simply convention for denoting a terminal command.


To use a fifo, you need both a reader and a writer (the POSIX standard does not define the behaviour from using the same file handle as both a reader and a writer so this library does not allow it).

Here's some example code that will simply write to a fifo and read from it, all in the same process:

reader ='path/to/fifo', :r, :nowait)
writer ='path/to/fifo', :w, :nowait)

writer.puts "Hello, world!"
#=> "Hello, world!\n"

Notice that we pass in :r and :w for the reader and writer respectively. Also, we have this :nowait symbol in there. This tells the library that we don't want to use "blocking" fifos.

Blocking vs Non-blocking

A blocking fifo will block the current thread of execution until the other end is opened. For example, the following code will never finish executing:

reader ='path/to/fifo', :r, :wait)
writer ='path/to/fifo', :w, :wait)

The thread will be blocked after the first line and it will wait until the writing end of the fifo is opened before allowing execution to continue. This also works exactly the same way in reverse (if you opened the writer before the reader).

The following code should work fine:

fork do
  reader ='path/to/fifo', :r, :wait)
  #=> Eventually, this will return "Hello, fork!\n"

fork do
  writer ='path/to/fifo', :w, :wait)
  writer.puts "Hello, fork!"


Alternately, you can use non-blocking pipes. These pipes don't wait for the other end to be open before doing there work. The following code will work just fine all in the same process:

writer ='path/to/fifo', :w, :nowait)
writer.puts "Testing"

reader ='path/to/fifo', :r, :nowait)
#=> "Testing\n"


Because of this, non-blocking is the default type of fifo that this library will create.

fifo ='path/to/fifo')
# This is a non-blocking reader by default

Other methods for reading and writing

There are other forms of reading and writing that will be familiar to you if you have used the Ruby File object:

reader ='path/to/fifo', :r, :nowait)
writer ='path/to/fifo', :w, :nowait)

writer.puts "Two", "Lines"
#=> "Two\n"
#=> "Lines\n"

writer.print "12345"
# reader.gets would block forever here, no new line
#=> "1"
#=> "2"
#=> "345"

#=> Blocks until something is written

writer.print "Same as puts\n"
#=> "Same as puts\n"
Something went wrong with that request. Please try again.