2010-11-18

I Like This Article Because It's Wrong

In reference to the enjoyable article "I Like Unicorn Because It's Unix".

I don't take any pleasure in claiming that the intelligent, read-worthy author is wrong. I don't even really mean it; the article has plenty of good stuff to say and has been a useful point of reference for me in weeks past. I have one point with which to quibble, and it may have been right when he first wrote it.

But, lest anyone else be led astray, here's the issue:


Most notable is the following call to select(2):

ret = IO.select(LISTENERS, nil, SELF_PIPE, timeout) or redo

This blocks until one of three things happen:
...
2. Some notable error state occurs on the file descriptor in SELF_PIPE (like when it’s closed), in which case the child’s side of the pipe is returned as an IO object. This really deserves its own essay, but I’ll take a quick shot: the IO object in SELF_PIPE is created in the parent process with pipe(2) (IO.pipe) before the children are forked off. The children then write on the pipe to achieve basic one-way IPC between child and master. It’s used here in the call to select(2) to detect the master going down unexpectedly – parent death causes the pipe to close. Unicorn children go down fast when their master dies.


That's not actually what happens.

SELF_PIPE is used in two different ways:

  1. In the master process, the master writes to the pipe from signal handlers; it selects on the pipe in its main loop, such that signals will wake the master up in that loop.

  2. In the child/worker processes, the original pipe inherited from the parent process is completely replaced by a new pipe; signal handlers in the child process close one end of the pipe, such that they interrupt the select referenced by Ryan Tomayko's blog as quoted above.



I admire Ryan Tomayko's blog and should he happen to stumble across this little article here, I hope he'll appreciate the tongue-and-cheek nature of the title. His article is more right than wrong, but that's not as much fun to claim, is it?

1 comment:

  1. Fork. Well, here's a classic; these are all Unix shell commands:
    look; gawk; find; sed; talk; grep; touch; finger; find; flex; unzip; head; tail; mount; workbone; fsck; yes; gasp; fsck; more; yes; yes; eject; umount; makeclean; zip; split;

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner

Subscribe (RSS)