Node:Duplicating Descriptors, Next:Descriptor Flags, Previous:Control Operations, Up:Low-Level I/O
You can duplicate a file descriptor, or allocate another file descriptor that refers to the same open file as the original. Duplicate descriptors share one file position and one set of file status flags (see File Status Flags), but each has its own set of file descriptor flags (see Descriptor Flags).
The major use of duplicating a file descriptor is to implement redirection of input or output: that is, to change the file or pipe that a particular file descriptor corresponds to.
You can perform this operation using the fcntl
function with the
F_DUPFD
command, but there are also convenient functions
dup
and dup2
for duplicating descriptors.
The fcntl
function and flags are declared in fcntl.h
,
while prototypes for dup
and dup2
are in the header file
unistd.h
.
int dup (int old) | Function |
This function copies descriptor old to the first available
descriptor number (the first number not currently open). It is
equivalent to fcntl (old, F_DUPFD, 0) .
|
int dup2 (int old, int new) | Function |
This function copies the descriptor old to descriptor number
new.
If old is an invalid descriptor, then If old and new are different numbers, and old is a
valid descriptor number, then close (new); fcntl (old, F_DUPFD, new) However, |
int F_DUPFD | Macro |
This macro is used as the command argument to fcntl , to
copy the file descriptor given as the first argument.
The form of the call in this case is:
fcntl (old, F_DUPFD, next-filedes) The next-filedes argument is of type The return value from
|
Here is an example showing how to use dup2
to do redirection.
Typically, redirection of the standard streams (like stdin
) is
done by a shell or shell-like program before calling one of the
exec
functions (see Executing a File) to execute a new
program in a child process. When the new program is executed, it
creates and initializes the standard streams to point to the
corresponding file descriptors, before its main
function is
invoked.
So, to redirect standard input to a file, the shell could do something
like:
pid = fork (); if (pid == 0) { char *filename; char *program; int file; ... file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY)); dup2 (file, STDIN_FILENO); TEMP_FAILURE_RETRY (close (file)); execv (program, NULL); }
There is also a more detailed example showing how to implement redirection in the context of a pipeline of processes in Launching Jobs.