tags:

views:

41

answers:

1

I have a question regading dup2. What does it exactly do? Copies the File Descriptor?

I don't quite understand the use of dup2. I tried google, and still don't quite understand what it means.

Thank you in advance.

+2  A: 

It takes two parameters that are descriptors. It makes the second one equivalent to the first one. You can use them interchangeably with a few minor exceptions... see the man page for more details on the use cases.

You would do this, for example, to redirect the console descriptors 1, 2, or 3, to some other device or file. The reason you don't want to close those then reopen, is that there is no guarantee you will receive the same number descriptor back in the next open call. That is why you would use open() to get a file descriptor, then dup2(new_descriptor, 1); to force descriptor 1 to go somewhere other than the console.

Amardeep