22

Do different threads within a single process have distinct independent file descriptor tables? If multiple threads within the same process concurrently access a single file, will the offset into the file for two different calls to open performed by different threads be thread-specific?

pants
  • 192
  • 13
Lipika Deka
  • 3,774
  • 6
  • 43
  • 56

4 Answers4

19

No, there is only one file descriptor table per process, and it's shared among all the threads.

From your problem description, you might want to look into the pread() and pwrite() functions.

janneb
  • 36,249
  • 2
  • 81
  • 97
11

The file descriptors are shared between the threads. If you want "thread specific" offsets, why not have each thread use a different file descriptor (open(2) multiple times) ?

cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

Try pread()/pwrite().

You can still share the same filedescriptor among multiple threads,i.e, parallel reads/writes to the same file is guaranteed to be atomic using pread()/pwrite() as you will need to specify offset and number of bytes to read/write respectively.

Aravindh
  • 535
  • 3
  • 9
0

In Linux, you can unshare() the file descriptor table via the CLONE_FILES flag, but I would advise against it.

ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • Note that this won't help with the "shared offsets" issue. Even if two threads are no longer sharing their fd table (and even if they're separate processes with separate fd tables to begin with), two file descriptors referring to the same "open file description" always share offsets. – R.. GitHub STOP HELPING ICE Aug 03 '12 at 02:07