Linked Channels (The GNU C Library)

#### 13.5.1 Linked Channels ¶

Channels that come from a single opening share the same file position; we call them linked channels. Linked channels result when you make a stream from a descriptor using `fdopen`, when you get a descriptor from a stream with `fileno`, when you copy a descriptor with `dup` or `dup2`, and when descriptors are inherited during `fork`. For files that don’t support random access, such as terminals and pipes, all channels are effectively linked. On random-access files, all append-type output streams are effectively linked to each other.

If you have been using a stream for I/O (or have just opened the stream), and you want to do I/O using another channel (either a stream or a descriptor) that is linked to it, you must first clean up the stream that you have been using. See Cleaning Streams.

Terminating a process, or executing a new program in the process, destroys all the streams in the process. If descriptors linked to these streams persist in other processes, their file positions become undefined as a result. To prevent this, you must clean up the streams before destroying them.

In addition to cleaning up a stream before doing I/O using another linked channel, additional precautions are needed to ensure a well-defined file position indicator in some cases. If both the following conditions hold, you must set the file position indicator on the new channel (a stream) using a function such as `fseek`.

- The new linked channel is a stream that was previously active.
- The file position indicator was previously set on that channel (while it was previously active) with a function such as `fseek`.

POSIX requires such precautions in more cases: if either the old or the new linked channel is a stream (whether or not previously active) and the file position indicator was previously set on any channel linked to those channels with a function such as `fseek` or `lseek`.