Threads and file descriptors

后端 未结 4 1362
萌比男神i
萌比男神i 2020-12-30 03:36

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,

相关标签:
4条回答
  • 2020-12-30 04:05

    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.

    0 讨论(0)
  • 2020-12-30 04:16

    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) ?

    0 讨论(0)
  • 2020-12-30 04:22

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

    0 讨论(0)
  • 2020-12-30 04:24

    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.

    0 讨论(0)
提交回复
热议问题