Due to "man 2 dup"
The call:

              fid = dup2(fildes, fildes2);

       shall be equivalent to:

              close(fildes2);
              fid = fcntl(fildes, F_DUPFD, fildes2);