Problems with working with Handles (PS)

Simon Marlow simonmar@microsoft.com
Fri, 13 Jun 2003 11:18:03 +0100


=20
> (moderator, can you please include this mail at the bottom of my=20
> previous mail?)
>=20
> PS: I think the next example shows pretty well what goes wrong when=20
> you're not closing the read-handle:
>=20
> -------
> test =3D do writeFile' "123.txt" "blaat"
>=20
>      appendFile' "123.txt" " 1"
>      z <- readFile' "123.txt"
>=20
>      appendFile' "123.txt" " 2"
>      s <- readFile' "123.txt"
>          appendFile' "123.txt" " 3"
>      t <- readFile' "123.txt"
>          putStr ("\n\n")
>      putStr (z ++ "\n" ++ s ++ "\n" ++ t)
>=20
> -------
>=20
> Instead of "blaat 1
>                 blaat 1 2
>                  blaat 1 2 3"
>=20
> three lines of "blaat 1 2 3" are outputted.

Note that on a conforming Haskell 98 system, the above program (with the
primes deleted) will fail.  For example, GHC responds:

Fail: resource busy
Action: openFile
Reason: file is locked
File: 123.txt

This is because Haskell 98 specifies that the IO system should implement
multiple-reader/single-writer locks on a per-file basis.

Cheers,
	Simon