LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to copy the file description when forking instead of sharing? (https://www.linuxquestions.org/questions/programming-9/how-to-copy-the-file-description-when-forking-instead-of-sharing-4175456669/)

Simple12 04-03-2013 06:00 AM

How to copy the file description when forking instead of sharing?
 
Hi all,
When a process forks, the child will share with its parent the file description(that includes the offset and the file status flag) that was open before the forking.

Is there a way to make the child have its own copy of the file description ?

I need that since I don't want both parent and child to share the same offset of the file; if one process has done a read, I don't want the offset of the file for the second process to be changed.


Thank you :)

mina86 04-03-2013 06:32 AM

pread() function may be of interest to you.

Simple12 04-03-2013 07:08 AM

Quote:

Originally Posted by mina86 (Post 4924202)
pread() function may be of interest to you.

pread() does not change the offset after the read. I want the offset to be changed independently in each process.

linosaurusroot 04-03-2013 08:03 AM

Some ideas here
http://stackoverflow.com/questions/1...another-access

Simple12 04-03-2013 08:23 AM

Quote:

Originally Posted by linosaurusroot (Post 4924267)

I couldn't get how this would help. basically, When the parent forks, I want the child to get the same copy of the open file entry of its parent. meaning if the parent before the fork has done some reads, I want the offset in the child to be the same as just before the fork. after the fork I want each processes to have its own copy of the offset.

mina86 04-03-2013 08:30 AM

Quote:

Originally Posted by Simple12 (Post 4924221)
pread() does not change the offset after the read. I want the offset to be changed independently in each process.

Depending what you are doing, you can maintain the offset by yourself.

Quote:

Originally Posted by Simple12 (Post 4924293)
I couldn't get how this would help. basically, When the parent forks, I want the child to get the same copy of the open file entry of its parent. meaning if the parent before the fork has done some reads, I want the offset in the child to be the same as just before the fork. after the fork I want each processes to have its own copy of the offset.

And your problem is?
Code:

pos = lseek(fd, 0, SEEK_CUR);
reopenFdUsingStackOverflowCode(fd);
lseek(fd, pos, SEEK_SET);


Simple12 04-03-2013 09:08 AM

Quote:

Originally Posted by mina86 (Post 4924300)
Depending what you are doing, you can maintain the offset by yourself.


And your problem is?
Code:

pos = lseek(fd, 0, SEEK_CUR);
reopenFdUsingStackOverflowCode(fd);
lseek(fd, pos, SEEK_SET);


Thank you, I got the idea :)


All times are GMT -5. The time now is 12:27 AM.