Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple file descriptors to the same file, C

I have a multithreaded application that is opening and reading the same file (not writing). I am opening a different file descriptor for each thread (but they all point to the same file). Each thread then reads the file and may close it and open it again if EOF is reached. Is this ok? If I perform fclose() on a file descriptor does it affect the other file descritptors that point to the same file?

like image 790
Gigi Avatar asked Apr 18 '10 21:04

Gigi


2 Answers

For Linux systems you don't need multiple file descriptors to do this. You can share a single file descriptor and use pread to atomically do a seek / read operation without modifying the file descriptor at all.

like image 164
gannon Avatar answered Sep 19 '22 10:09

gannon


That's ok. You can open all times you want the same file and each file descriptor will be independent from each other.

like image 43
Pablo Santa Cruz Avatar answered Sep 23 '22 10:09

Pablo Santa Cruz