Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many independent file pointers/positions exist on Windows?

Tags:

c

file

windows

If I open a single file (using CreateFile()) two times inside a single thread such that I have two valid handles at once, will the two file handles have a shared "file pointer" (SetFilePointer()), or will the two handles have separate independent "file pointers"?

What if there are instead two concurrent threads in one process, and they each hold one handle to the same file. Will those two handles have independent file pointers?

like image 404
Kristian Spangsege Avatar asked Dec 11 '25 14:12

Kristian Spangsege


2 Answers

Each time a thread opens a file, a new file object is created with a new set of handle-specific attributes. For example, the current byte offset attribute refers to the location in the file at which the next read or write operation using that handle will occur. Each handle to a file has a private byte offset even though the underlying file is shared. A file object is also unique to a process, except when a process duplicates a file handle to another process (by using the Windows DuplicateHandle function) or when a child process inherits a file handle from a parent process. In these situations, the two processes have separate handles that refer to the same file object. Windows Internals 5th

like image 143
sergmat Avatar answered Dec 14 '25 09:12

sergmat


Distinct file handles have distinct file pointers, so these scenarios will work without issue (for example, two threads can read from different sections of the same file "concurrently" as long as each uses its own file handle exclusively).

like image 43
Jon Avatar answered Dec 14 '25 09:12

Jon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!