the secret life of zombies

jsnx jason.dusek at
Fri Oct 12 03:37:15 CEST 2007

I'd like a program, call it 'pipe-fitter', that connects and
interactive app
to three named pipes. The program will filter out EOF from the input
pipe, to
keep the interactive program alive for multiple commands. When the
app dies, by receiving 'exit', say, then we are done with it -- pipe-
bails out.

I've tried writing pipe-fitter in the shell (infinite loop), C (could
not get
off the ground at all with this one) and now Python. After trying a
different approaches, I was turned on to the subprocess module by
someone on
IRC. My program is almost right, except for damn zombies. Here is pipe-

Say we make some fifos in a dir 'pipes'

  mkdir pipes
  mkfifo pipes/i pipes/o pipes/e

Now we call pipe-fitter thusly: -d pipes sh -xv

This runs `sh -xv` with stdin, stdout and stderr connected to pipes/i,
and pipes/e respectively. We open three new terminals. In the first
one, we

  echo "ls -l" > pipes/i

and in the second:

  cat pipes/o

and in the third:

  cat pipes/e

Once we enter the final command, the 'echo' returns and the text comes
streaming out in the 2nd and 3rd terminals. Wonderful. Now, let's try

  echo 'exit' > pipes/i

As expected, cat will terminate in the 2nd and 3rd terminals, with the
terminal displayin:

  + exit

However, is still running. Why is that? It did not
'poll' at
the right time, it seems. If I try stuffing more input into pipes/i, I

  IOError: [Errno 32] Broken pipe

and finally quits. Not exactly what I was hoping for.

Prowling around with PS, I find that the `sh -xv` called from within
pipe-fitter is a zombie for a spell; if pipe-fitter fails to catch it
at the
right time, there is no avoiding the IOError. I'm using
subprocess.Popen.poll() in a loop, to catch the process's termination
-- is
there something else I should be doing?

More information about the Python-list mailing list