On Tue, Sep 29, 2009 at 04:45:28PM +0530, krushnaal pai wrote: > each process has its own 'fd' array isnt it?
What fd array?...
> so a fd coming from another process doesnt make sense to the running process
Why not? The file descriptor is just an integer. What the kernel does
with it is its business.
Like this:
a = open("file", O_RDWR);
b = dup(a);
c = open("file", O_RDWR);
Let's assume the last open file descriptor before this was stderr, 2.
Then you get:
a = 3; b = 4; c = 5;
Now, there are two structures (simplified) in the kernel. A structure
for the file on disk, and a structure for the file descriptor (a pointer
to the file structure, flags, mode, offset).
So a and b point to the same file descriptor structure, while c points
to a new one, both pointing to the same file structure.
If you send file descriptor a to a second process, that one will get a
new file descriptor, like, say, 7.
It will point to the same file descriptor structure as file descriptor a
in the original process.
That's the only portable way to do this.
Remember that processes don't have direct access to any of those
structures, only to the integer values.
--
Luciano Rocha <[email protected]>
Eurotux Informática, S.A. <http://www.eurotux.com/>
pgpowBt8gRWqa.pgp
Description: PGP signature
