1

When a process forks, the child will share with its parent the file description that was open before the forking.

Is there a way to make the child have its own copy of the file description (that includes the offset and the file status flag)?

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.

4

1 回答 1

0

既有“打开文件描述符”又有“打开文件描述”。使用 打开文件时open(),会同时创建文件描述符和文件描述。当一个文件描述符被复制(dup()dup2())时,这两个描述符引用相同的打开文件描述。

fork(),父级中的文件描述符被复制(复制)到子级中;parent 和 child 中的描述符都指向相同的打开文件描述。

子进程应拥有自己的父文件描述符副本。每个子文件描述符都应与父文件描述符对应的打开文件描述相同。

否则没有办法让它工作——您必须关闭并重新打开文件才能获得单独的打开文件描述。

于 2013-04-02T19:40:41.600 回答