|
dup is a system call vs. dup2 which is a
C library call. As I gather you alreay know,
dup creates a new file descriptor that duplicates
the file descriptor given as the argument:
int dup(int fildes);
...the point here being that the returned file descriptor that the system call returns to you
is the next lowest number available. For instance,
you program has stdin (fd=0) stdout (fd=1) and
stderr (fd=2) and you use: newfd = dup(2);
now newfd = 4 but has all the attributes of
fd 2 (stderr).
The C library call dup2 has 2 arguments...
int dup2(int fildes, int fildes2);
...by using: dup2(1, 2);
the stderr fd 2 will first be closed then opened
(re-created) with all the attributes of file
descriptor 1 (stdout). Basically, it gives you
control over what your new fd number will be.
This isn't a very useful example but
I hope this answers your question.
|