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.