Threads and file descriptors

2020-02-26 06:44发布

Am sorry for not doing my own research and asking it here. I am slightly short of time.

Is it possible to have two file descriptor table for two or more threads spwaned from a single thread. The two or more child threads are concurrently accessing same file, so is the offset for two different opens for same file from different threads, thread specific?

Thanks

标签: c linux pthreads
4条回答
我命由我不由天
2楼-- · 2020-02-26 06:57

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

查看更多
可以哭但决不认输i
3楼-- · 2020-02-26 07:00

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.

查看更多
The star\"
4楼-- · 2020-02-26 07:01

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

查看更多
Luminary・发光体
5楼-- · 2020-02-26 07:05

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.

查看更多
登录 后发表回答