Using multiple file descriptors for the same file

D

DJ Dharme

Hi all,
I am writing a multi-threaded application in c++ running 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? Pardon me if I am
posting this in a wrong group.

Thanks!

DJD.
 
M

Michael

DJ said:
Hi all,
I am writing a multi-threaded application in c++ running 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? Pardon me if I am
posting this in a wrong group.

Thanks!

DJD.

This is an OS issue, not c++ issue. I think that you may post in
comp.unix.solaris
 
M

Maxim Yegorushkin

          I am writing a multi-threaded application in c++ running 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top