named pipes

Albert Y. C. Lai trebla at vex.net
Sat Jan 21 02:17:10 CET 2012


On 12-01-19 02:16 PM, Serge D. Mechveliani wrote:
> -- Main.hs ---------------------------------------------------------
>             hPutStr h1 str
>             hFlush h1

This fails to send a newline.

> ----------------------- fifo2.c -------------------------------------
>    if (fgets(str, BOUND, toA) == NULL) {

This refuses to finish until it sees a newline (or BOUND chars, or the 
eof condition, or an error condition).

What really interests me is that if Main.hs is linked with -threaded, 
the deadlock is unlocked, and both programs proceed to completion.

This is because the -threaded runtime goes through garbage collection 
when idling (such as waiting for hGetLine). Since there is no further 
reference to h1 by then, GC hCloses it. That unlocks the fgets side.

This is fragile and not to be relied on. You will always have further 
references to h1 in practice.

> Comparison with C - C
> ---------------------
> If both programs are in C, then the whole loop of the string exchange
> (with  fifo2.c  rewritten into a loop) works as needed, both for the
> variants with fgets and with `read'.

I am not convinced in the case of fgets. I cannot reproduce it. (Oh, I 
know read has a much better chance, it doesn't wait for a newline.)



More information about the Glasgow-haskell-users mailing list