简体   繁体   中英

Unix C dup2 Question

I have a question regarding 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM