How many independent pointers / file positions are there on Windows?

If I open the same file (using CreateFile ()) twice in the same stream, so that I have two valid handles at the same time, will the two file descriptors have a common "file pointer" (SetFilePointer ()) or will the two handles have separate independent "file pointers"?

What if there are two parallel threads in the same process, and each of them holds one descriptor in the same file. Will these two handles have independent file pointers?

+3


source to share


3 answers


Each time a stream opens a file, a new file object is created with a new set of descriptor-specific attributes. For example, the current byte offset attribute refers to the location in the file where the next read or write operation will be performed using this handle. Each file descriptor has a private byte offset, although the main file is shared. A file object is also unique to a process, unless the process duplicates a file descriptor to another process (using the DuplicateHandle> function ) or when a child process inherits a file descriptor from a parent process. In these situations, the two processes have separate descriptors referring to the same file. Windows Internals 5th



+3


source


Distinguishing file descriptors have different file pointers, so these scripts will work without problems (for example, two threads can read from different sections of the same file "simultaneously" if each uses its own file descriptor exclusively).



+2


source


File descriptors created by various calls CreateFile

have independent file pointers. You can use them in separate threads as you plan.

Handles are duplicated DuplicateHandle

swapped file pointer: do not use it to get a "separate" file descriptor to be used on another thread.

+1


source







All Articles