C

Associate
Joined
26 Jan 2006
Posts
1,502
Hello,

Anyone has any links that explain how file pointers with parent and child (fork) work?

I know that if you fork then, the child gets a clone of the parent (code, variables and pointers!) so if you fork after you opened the file, then both parent and child share the pointer?

However, even if you fork and then assign the file pointer, still both pointers point to the same file? Is there an "internal" pointer in this case were keeps track on which line the process is reading from?

Thanks.
 
Hello,

Anyone has any links that explain how file pointers with parent and child (fork) work?

I know that if you fork then, the child gets a clone of the parent (code, variables and pointers!) so if you fork after you opened the file, then both parent and child share the pointer?

However, even if you fork and then assign the file pointer, still both pointers point to the same file? Is there an "internal" pointer in this case were keeps track on which line the process is reading from?

Thanks.


Not 100% sure on this, but here goes:

Yes, if you opened say 'foo.txt', pointed to by 'fp' and then forked immediately afterwards both parent and child's version of 'fp' will point to the beginning of foo.txt,

With your second bit, I believe when you do a command like a read() (or whatever higher level call to get a char/string from the file) then the pointer itself changes to show the position in a file, hence why you can do things like while loops which just read again and again from the file, I believe the pointer in that instance is pointing to a marker in the file of almost where a cursor is...

I think, haven't done file handling in ages for the last bit may be gibberish, first bits almost definately right though :p
 
Back
Top Bottom