Linux dup() Shares File Offsets: Use pread() for Independent Range Reads
Reproduce shared offsets with dup(), read explicit file ranges with pread(), and understand EOF handling, concurrent modification, and memory limits.
When several workers read different ranges of the same file on a Linux server, duplicating its descriptor with dup() does not give each worker an independent read position. For range-based work, use an operation such as pread() that accepts an offset on every call. This article reproduces the shared position with Python's low-level file APIs and builds a helper that reads a complete range or reports an early EOF explicitly.
Two reading devices share a cursor over a single data strip while a separate light illuminates another region. A conceptual illustration of shared offsets with dup and explicit range reads with pread.
Written and documentation checked: 2026-11-26. Tested environment: Linux x86_64, CPython 3.12.14. Only the standard library is required. The linked Python 3.12 documentation displayed version 3.12.16 when checked; that is distinct from the version actually used to run the example.
Different descriptor numbers can share one position
Suppose a file descriptor is duplicated so that workers can read separate portions of a file. Read two bytes through the original and then two through the duplicate. The duplicate does not start at the beginning. It continues after the first read.
The reason is that a file descriptor and an open file description are different things. dup() creates another descriptor referring to the same open file description, so the descriptors share the file offset and file status flags. Calling lseek() through either descriptor also changes the position observed through the other. See the Linux dup(2) manual.
Giving every worker a lseek(fd, offset) followed by read(fd, length) does not resolve this on its own: another worker can change the shared position between those calls. This is an ordering problem inferred from the shared API state, not a claim that the experiment below measured a timing-dependent race.
Choose between range reads and independent sequential reads
| Approach | Read position | Suitable use |
dup() followed by read() | Shared with the original | Another descriptor for the same open file |
| A separate os.open() followed by read() | Separate in each open file description | Each worker needs its own sequential traversal state |
os.pread(fd, n, offset) | Reads at the supplied offset without changing the current offset | Every task has a defined byte range |
A new open() call creates a new open file description. However, reopening a pathname can encounter a different file if that path has been replaced. The Linux open(2) manual explains that an existing open reference is unaffected by subsequent removal or changes to the pathname. If the design must keep the same open target, retain the existing descriptor.
Python's os.pread() returns the bytes read and leaves the current file offset unchanged. Its documented availability is Unix. The example uses descriptors returned by os.open() throughout, without mixing in buffered file-object read() calls.
Reproduce the behavior and read a complete range
Save the following as dup_pread_demo.py. It creates only the six bytes ABCDEF in a temporary directory and cleans up its files and descriptors on exit.
pread_exact() advances its explicit offset by the number of bytes actually read. A single pread() may return fewer bytes than requested, and an empty result indicates EOF. The helper therefore raises an exception if it reaches EOF before collecting the required bytes. These return rules and the requirement for a seekable file are documented in the Linux pread(2) manual.
The 64 KiB cap limits each individual request, not the helper's total memory use. Because it assembles the final result in memory, a service should bound the input length or consume chunks as they arrive. A zero length returns an empty result; negative ranges are rejected.
Interpret the execution results
The actual output in the environment stated above was as follows. Every assert in the program passed.
The first two lines show that reading and seeking through the duplicate affect the original position. The third confirms that reading EF leaves the original offset at zero. The separately opened descriptor reads A and advances only its own offset to one. The two tasks submitted to the thread pool returned ABC and DEF, respectively, while the original offset remained zero. Results are collected in submission order; the experiment did not measure whether the tasks actually overlapped in execution.
The checks also covered rejection of a range extending past EOF, a negative offset, and a negative length, as well as an empty range. The experiment did not force a short read on a regular file or test concurrent writes, truncation, or performance. It is a reproduction of file-position semantics.
What pread solves
If a server assigns defined ranges of an unchanging file to multiple tasks, pread() allows position to be expressed in each task's arguments. This reduces the need to treat a shared-position lseek() and read() pair as one logical operation.
However, avoiding offset interference is separate from providing a content snapshot. If the same file is modified or shortened, content may change between reads, or EOF may arrive earlier than expected. Consistency across ranges requires an immutable file version or a separate synchronization policy. This is a design consideration about applicability, not a concurrent-modification behavior verified by this example.
Keep the descriptor open until all tasks finish. The helper does not manage another thread closing it and the descriptor number being reused. It is also not a solution for non-seekable pipes or sockets, and this example was not tested on Windows. Start by deciding whether a task needs independent sequential state or an explicit byte range, without treating dup() as an independent cursor.