2010-11-25 12 views
22

たとえば、open()というファイルを開きます。次に私はfork()私のプログラムです。fork()の実行時にファイル記述子が共有されていますか?

父と子はファイルディスクリプタに同じオフセットを共有するようになりますか?

私は私の父の書き込みをする場合は、オフセットも子供に変更されますか?

fork()の後にオフセットは独立していますか?

答えて

28

:ファイルが開かれているところ

* The child inherits copies of the parent’s set of open file descrip- 
    tors. Each file descriptor in the child refers to the same open 
    file description (see open(2)) as the corresponding file descriptor 
    in the parent. This means that the two descriptors share open file 
    status flags, current file offset, and signal-driven I/O attributes 
    (see the description of F_SETOWN and F_SETSIG in fcntl(2)). 
+1

これは依存しませんか?フォークの後、またはその前にopen(filename、int ..)コールがあった場合の意味です。 – ArmenB

+1

@Armen:はい、そうです。 –

+0

これは奇妙なことですが、ファイルopen文がforkの後に作成された場合、2つの異なるファイル記述子があります。しかし、私がfcntlを使ってファイルをロックしようとすると、動作しません。子と親の両方がロックを無視します – ArmenB

2

これらは同じオフセットを共有します。 fork(2)から

関連する問題