Question

Ryan Tomayko touched off quite a fire storm with this post about using Unix process control commands.

We should be doing more of this. A lot more of this. I'm talking about fork(2), execve(2), pipe(2), socketpair(2), select(2), kill(2), sigaction(2), and so on and so forth. These are our friends. They want so badly just to help us.

I have a bit of code (a delayed_job clone for DataMapper that I think would fit right in with this, but I'm not clear on how to take advantage of the listed commands. Any Ideas on how to improve this code?

def start
  say "*** Starting job worker #{@name}"
  t = Thread.new do
    loop do
      delay = Update.work_off(self)
      break if $exit
      sleep delay
      break if $exit
    end
    clear_locks
  end

  trap('TERM') { terminate_with t }
  trap('INT')  { terminate_with t }

  trap('USR1') do
    say "Wakeup Signal Caught"
    t.run
  end
end
Was it helpful?

Solution

Ahh yes... the dangers of "We should do more of this" without explaining what each of those do and in what circumstances you'd use them. For something like delayed_job you may even be using fork without knowing that you're using fork. That said, it really doesn't matter. Ryan was talking about using fork for preforking servers. delayed_job would use fork for turning a process into a daemon. Same system call, different purposes. Running delayed_job in the foreground (without fork) vs in the background (with fork) will result in a negligible performance difference.

However, if you write a server that accepts concurrent connections, now Ryan's advice is right on the money.

  • fork: creates a copy of the original process
  • execve: stops executing the current file and begins executing a new file in the same process (very useful in rake tasks)
  • pipe: creates a pipe (two file descriptors, one for read, one for write)
  • socketpair: like a pipe, but for sockets
  • select: let's you wait for one or more of multiple file descriptors to be ready with a timeout
  • kill: used to send a signal to a process
  • sigaction: lets you change what happens when a process receives a signal

OTHER TIPS

5 months later, you can view my solution at http://github.com/antarestrader/Updater. Look at lib/updater/fork_worker.rb

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top