[Haskell-cafe] Opening the same file multiple times

Einar Karttunen ekarttun at cs.helsinki.fi
Mon Dec 12 09:01:20 EST 2005

On 12.12 12:06, Duncan Coutts wrote:
> It states in the Haskell Report 21.2.3:
> http://haskell.org/onlinereport/io.html

Thanks, for the pointer, but am looking for an extension
in the non-haskell98 API to do it.

It seems that things are quite problematic:

1) Use openFile or GHC.Handle.openFd

Works in Hugs, fails as the standard mandates in GHC
due to locking. This is fine.

2) Use openFile + handleToFd + unlockFile

This seems like a good plan. Except handleToFd will close the Handle.

3) Using System.Posix.IO

Using the fd{Read,Close,Write} functions from System.Posix.IO
could solve the problem - except that there is no way to
write binary buffers (Ptr Word8) with the API. Thus no

4) Use System.Posix.IO.openFd + fdToHandle

This appears to be nice on surface. Except fdToHandle locks
the file, thus back to drawing board.

5) Use System.Posix.IO.openFd + fdToHandle + unlockFile

Thus we have:

* lock mutex - otherwise there is a race condition
* System.Posix.IO.openFd - open the file emulating openFile
* fdToHandle - convert the file to Handle locking it
* unlockFile (fromIntegral fd) - now unlock the original fd
* unlock mutex

Is this really the most simple way of doing things?
Most of the operations will also hit the disk, and
be slow (safe) FFI calls.

- Einar Karttunen

More information about the Haskell-Cafe mailing list