Re: Using multiple file descriptors for the same file
On Oct 20, 10:54 am, DJ Dharme <donjuandharmap...@gmail.com> wrote:
I am writing a multi-threaded application in c++ runn=
ing on
solaris. I have a file which is updated by a single thread by
appending data into the file and at same time the other threads are
reading the content written into the file. Can anybody tell me is
there a performance or any other gain (except for the multex locking)
by using different file descriptors in each thread for the same file
rather than using a single FD with a mutex (or read write) lock. Is it
an overhead using multiple FDs for a single file?
It depends on how you create file descriptors on the same file.
If you use open() with the same file name, you'll get file descriptors
referring to a different file description, referring to the same file.
fd0 -> description0 \
fd1 -> description1 -> file
fd2 -> description2 /
If, on the other hand, you use dup() to get new file descriptors,
these file descriptors refer to the same file description.
fd0 \
fd1 -> description -> file
fd2 /
File description is a structure where file offset and access mode are
stored among other things. This structure is protected by a mutex (or
a spin-lock).
In the latter case the threads will contend to access the same file
description when you do read/write().
Pardon me if I am posting this in a wrong group.
Better use comp.unix.programmer for Unix specific questions. Replies
to this message should automatically go there.
--
Max