What do the dup() and dup2() systems really do?

Both make a new file descriptor corresponding to an existing open file description. Most properties between the old and new fd (like position) are shared; the only property I can think of that’s not shared is the close-on-exec flag. The difference between dup and dup2 is that dup assigns the lowest available file descriptor number, while dup2 lets you choose the file descriptor number that will be assigned and atomically closes and replaces it if it’s already taken.

Leave a Comment