[Haskell-cafe] named pipe interface

Donn Cave donn at avvanta.com
Sat Jan 14 17:57:56 CET 2012

Quoth "Serge D. Mechveliani" <mechvel at botik.ru>,

> By  openFile  you, probably, mean  openFd.

Yes, sorry!

> Another point is the number of open files, for a long loop.
>   toA_IO   = openFd "toA"   WriteOnly Nothing defaultFileFlags
> When applying  axiomIO  in a loop of 9000 strings, it breaks:
> "too many open files".
> I do not understand why it is so, because  toA_IO and fromA_IO  are 
> global constants (I have not any experience with `do').

toA_IO is a global constant of type "IO Fd", not "Fd".  You now see
the importance of this distinction - the action actually transpires at
"toA <- toA_IO", and each time that executes, you get a new file

> Anyway, I have changed this to
>   toA   = unsafePerformIO toA_IO
>   fromA = unsafePerformIO fromA_IO
> (I need to understand further whether my usage of  unsafePerformIO  
> really damages the project).

It's actually similar to the way some libraries initialize global
values, but there are some additional complexities and it isn't
clear to me that it's all guaranteed to work anyway.  You can read
much more about this here:
I'm no expert in this, but they're sure here on haskell-cafe, so
if you want to take this up, you might start a new topic here,
something like "global initialization with unsafePerformIO",
describe what you're doing and explain why you can't just pass
the open file descriptors as function parameters.

> Indeed. Initially, I tried  C <-> C,  and used  fgets, fputs, fflush.
> And it did not work, it required to open/close files inside a loop;
> I failed with attempts. Again, do not understand, why (do they wait
> till the buffer is full?).

I don't know.  When I was younger, I used to track these problems down
and try to explain in detail why buffered I/O is a bad bet with pipes,
sockets etc.  I don't think anyone listened.  I think I am going to
experiment with "I am old, so listen to me" and see if it works any better.


More information about the Haskell-Cafe mailing list