4

What do the dup() and dup2() system calls really do? How would I use them in practice?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user484457
  • 4,301
  • 7
  • 21
  • 14

1 Answers1

5

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711