[Haskell-cafe] Re: process

Bryan O'Sullivan bos at serpentine.com
Fri Feb 23 13:30:16 EST 2007


Dougal Stanton wrote:

>> If it basically works, what goes wrong in my programm?
> 
> Maybe something to do with compiler flags?

No.  This isn't even a Haskell-related problem, in all likelihood.

Bidirectional interaction with another process over a pipe, particularly 
when the other process is using stdio or an equivalent (i.e. most 
programs), is a classic and fruitful source of deadlocks.

Just because *your* end of each pipe is a line-buffered file handle has 
no bearing on the *other* process's management of its pair of endpoints. 
  For example, on a Unix-like system, the other process's stdio will 
block-buffer stdin and stdout by default if it finds that they're not 
attached to tty-like file descriptors.

There are really only two ways to deal with this.  The first is to read 
from the subprocess in a separate thread, but this only works 
effectively if what you're sending to the other process doesn't depend 
on what you read back from it (because there's no way of forcing it to 
send you anything).

The second is Unix-specific, and involves talking to the other process 
via a pseudotty instead of a pair of pipes.  This convinces the other 
process's stdio that you're a terminal, and you get the line-buffering 
you desire.  It's *still* highly deadlock-prone, and not something to do 
casually.

So what you're trying to do looks easy if you've never tried it, but 
it's actually very fiddly in all but the most trivial of circumstances.

The third, and best, way to deal with this problem is to completely 
avoid it unless you want to spend several hours or days scratching your 
head.

	<b


More information about the Haskell-Cafe mailing list