Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads and file descriptors

Tags:

c

linux

pthreads

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?

like image 467
Lipika Deka Avatar asked Jun 03 '11 06:06

Lipika Deka


People also ask

Do threads share file descriptors?

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

Are file descriptors thread safe?

Any system level (syscall) file descriptor access is thread safe in all mainstream UNIX-like OSes.

What are file descriptors used for?

A file descriptor is a number that uniquely identifies an open file in a computer's operating system. It describes a data resource, and how that resource may be accessed. When a program asks to open a file — or another data resource, like a network socket — the kernel: Grants access.

What does file descriptor means?

A file descriptor is an unsigned integer used by a process to identify an open file. The number of file descriptors available to a process is limited by the /OPEN_MAX control in the sys/limits. h file. The number of file descriptors is also controlled by the ulimit -n flag.


1 Answers

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.

like image 169
janneb Avatar answered Oct 12 '22 00:10

janneb