In Unix-like operating systems ,. Both dup2 and dup generate a copy of filedescriptor through system calls. Dup is quite simple for me: intdup (intfiledes); dup2 is a bit confused about intdup2 (intfiledes1, intfiledes2); in fact, declaration is better under intdup2 (effecldfd, intnewfd ).
In Unix-like operating systems ,. Both dup2 and dup generate a copy of file descriptor through system calls. Dup is also very simple for me: int dup (int filedes); dup2 is a bit confused about int dup2 (int filedes1, int filedes2); in fact, declaration is better than int dup2 (int oldfd, int newfd) below
In Unix-like operating systems ,. Both dup2 and dup generate a copy of file descriptor through system calls.
Dup is quite simple for me
int dup(int filedes);
Dup2 is a bit confused.
int dup2(int filedes1,int filedes2);
In fact, declaration is better.
int dup2(int oldfd,int newfd)
The following is an explanation provided by apue.
With dup2, we specify the value of the new descriptor with the fd2 argument.
If fd2 is already open, it is first closed. If fd equals fd2, then dup2 returns fd2 without closing it.
Otherwise, theFD_CLOEXECfile descriptor flag is cleared forfd2, so that fd2 is left open if the process CILS exec
I am confused. What if newfd is one of STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, (0, 1, 2?
Does dup2 return an error or "fd2 is already open, it is first closed?
Just test it.
#include#include"fcntl.h"#include
int main(){ int file_descriptor; if((file_descriptor = open("./text.t",O_RDWR)) < 0) { printf("error\n"); } else { printf("file descriptor is %d\n",file_descriptor); } printf("dup2 return value:%d\n dup return value\ %d\n",dup2(file_descriptor,0),dup(file_descriptor)); printf("dup2 return value:%d\ndup return value\ %d\n",dup2(file_descriptor,1),dup(file_descriptor)); printf("dup2 return value:%d\ndup return value\ %d\n",dup2(file_descriptor,2),dup(file_descriptor)); close(file_descriptor); return 0;}
Aha! Something interesting happened.
Running result:
File descriptor is 3
Dup2 return value: 0
Dup return value 4
The above is the running result of this code. Well... I have three printf statements. Why does mi only accept the first one, WTF...
Dup2 called in the second printf statement
What is the second parameter of dup2? The essence of the STDOUT_FILENO macro is 1. Here is the sentence "fd2 is already open, it is first closed"
The three standard streams must be opened at the beginning of the file process, and already open is a thing.
So... Dup2 shut down STDOUT_FILENO In the second printf very honestly.
At this time, the output of printf will not be printed on the standard output device, because STDOUT_FILENO has been disabled !!
In this case, you only need to change the second dup2 parameter in the second printf to a number greater than 2.
Code:
#include#include"fcntl.h"#include
int main(){ int file_descriptor; if((file_descriptor = open("./text.t",O_RDWR)) < 0) { printf("error\n"); } else { printf("file descriptor is %d\n",file_descriptor); } printf("dup2 return value:%d\n dup return value\ %d\n",dup2(file_descriptor,0),dup(file_descriptor)); printf("dup2 return value:%d\ndup return value\ %d\n",dup2(file_descriptor,10),dup(file_descriptor)); printf("dup2 return value:%d\ndup return value\ %d\n",dup2(file_descriptor,11),dup(file_descriptor)); close(file_descriptor); return 0;}
Running result:
File descriptor is 3
Dup2 return value: 0
Dup return value 4
Dup2 return value: 10
Dup return value 5
Dup2 return value: 11
Dup return value 6
It is clear at this time.
Similarly, the call
Dup2 (fd, fd2 );
Is equivalent
Close (fd2 );
Fcntl (fd, F_DUPFD, fd2 );
Of course they are not exactly the same. The differences are as follows:
1. dup2 is an atomic operation, whereas the alternate form involves two function
CILS. It is possible in the latter case to have a signal catcher called between
Close and the fcntl that cocould modify the file descriptors. (We describe
Signals in Chapter 10.) The same problem cocould occur if a different thread
Changes the file descriptors. (We describe threads in Chapter 11 .)
2. There are some errno differences between dup2 and fcntl.
Update 2014.04.10
Today I have a "deeper" understanding.
int dup2(int filedes1,int filedes2);
It is to disable the stream that filedes2 points to and redirect to the stream that filedes1 points to. In this case, both filedes1 and filedes2 point to the same stream.
That's simple ....