Allocate a new descriptor that refers to a file specified by a file descriptor returned by open, creat, or pipe. Format #include <unistd.h> int dup (int file_desc1); int dup2 (int file_desc1, int file_desc2);
1 – Arguments
file_desc1 The file descriptor being duplicated. file_desc2 The new file descriptor to be assigned to the file designated by file_desc1.
2 – Description
The dup function causes a previously unallocated descriptor to refer to its argument, while the dup2 function causes its second argument to refer to the same file as its first argument. The argument file_desc1 is invalid if it does not describe an open file; file_desc2 is invalid if the new file descriptor cannot be allocated. If file_desc2 is connected to an open file, that file is closed.
3 – Return Values
n The new file descriptor. -1 Indicates that an invalid argument was passed to the function.