file descriptors and handles

Hi all, Today I had a look at chapter 20 of RWH. The extended example (stripped down HSH) in the end is great. I think I understand it, but I have some questions left: the master process closes the client-sided FD's. it uses fdToHandle for the other sides of the pipe to get a handle to stdIn and stdOut on the client process. it writes to the stdIn handle and closes it afterwards. - doesn't it need to close the fd as well? the stdOut handle gets used when reading the command output (using hGetContents) - doesn't the handle need to get closed? or will this happen automatically if hGetContents finds EOF ? - and also here, shouldn't the fd get closed? More general: what happens when closing a handle when there's still output left? Will reading further output throw an exception or just think it's EOF? And what happens when closing the underlying FD? will the handle "break"? or will it close automatically? Thanks for any help Mathijs

On Mar 8, 2010, at 14:26 , Mathijs Kwik wrote:
it writes to the stdIn handle and closes it afterwards. - doesn't it need to close the fd as well?
Once you've made a Handle from it, the Handle "owns" it; in particular, when the Handle is finalized it will close the fd. Also, attempting to use the fd for anything directly is likely to cause severe confusion to the process or file on the other end.
the stdOut handle gets used when reading the command output (using hGetContents) - doesn't the handle need to get closed? or will this happen automatically if hGetContents finds EOF ?
hGetContents automatically closes the Handle lazily.
Will reading further output throw an exception or just think it's EOF? And what happens when closing the underlying FD? will the handle "break"? or will it close automatically?
If you manually close the fd, the Handle will become unhappy and throw an exception. Don't touch an fd after performing an fdToHandle, or a Handle after performing a handleToFd (unless you are very certain about what you're doing, which is likely to require knowledge of how the GHC runtime's Handles work). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (2)
-
Brandon S. Allbery KF8NH
-
Mathijs Kwik