June 16, 2010

The Self-Pipe Trick


Unicorn is Unix mentions the use of the self-pipe trick, mixing select with Unix signals. D. J. Bernstein, the creator of daemontools states he came up with it here. The problem is that a signal handler might be called while select is starting up. The solution is to select for read on a pipe and write to the pipe from within the signal handler.

  # self_pipe example in Ruby
  SELF_PIPE = IO.pipe
  ['INT', 'TERM'].each { |sig| trap(sig) {shutdown}}

  def shutdown
    SELF_PIPE[1].puts '.'
  end

  selected = select(SELF_PIPE, nil, nil, nil)
  puts "SELF_PIPE success" if selected[0][0] == SELF_PIPE[0]

 

This is the personal blog of Simon Horne. I currently work as a Software Developer for Streetline Networks in San Francisco.




Copyright © 2010 Simon Horne

About This Website

This website is primarily a demonstration of various tools, technologies and techniques used in the construction of a simple website.

Contact Details