简体   繁体   中英

Threads and file descriptors

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?

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

Try pread()/pwrite().

You can still share the same filedescriptor among multiple threads,ie, 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM