[Haskell-cafe] Re: I/O interface

Keith Wansbrough Keith.Wansbrough at cl.cam.ac.uk
Fri Jan 14 08:18:00 EST 2005

> First of all, I don't think any OS shares file pointers between 
> processes. Otherwise it would be practically impossible to safely use an 
> inherited filehandle via any API. Different threads using the same 
> filehandle do share a file pointer (which is a major nuisance in my 
> experience, because of the lack of an atomic seek-read/write), but a 
> Posix fork duplicates the file pointer along with all other state. I 
> can't believe I'm wrong about this, but someone please correct me if I am.

I'm afraid you _are_ wrong :-(... POSIX quite clearly states that the 
descriptors are duplicated by fork(), but the "open file descriptions"
(your "file pointers") are shared.  This is obvious by observing what
happens when you write to the same fd in parent and child after a
fork: you don't end up overwriting, you end up interleaving.

--KW 8-)

Keith Wansbrough <kw217 at cl.cam.ac.uk>
University of Cambridge Computer Laboratory.

More information about the Haskell-Cafe mailing list