In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to redirect and restore dup and dup2 functions. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Before we get to the point, let's take a look at the use of dup and dup2 functions.
Both dup and dup2 are system calls and can be used to copy file descriptors.
# include
Int dup (int oldfd)
Copy an old file descriptor and return the new file descriptor, and the two file descriptors share the same file pointer and point to the same file.
The default file descriptor returns the smallest and unused.
Int dup2 (int oldfd, int newfd)
Like dup, except that you specify a new file descriptor.
If the call succeeds, a new file descriptor is returned.
Redirection in shell "
< " (输入) 和 " >"(output) is implemented with the dup function.
Let's look at a piece of code.
# include # include int main (int argc, char * argv []) {int oldfd; oldfd = open ("a.txt", O_RDWR | O_CREAT | O_APPEND, 0666); / / Open file if (old = =-1) {printf ("creation file failed\ n"); exit (- 1) } else {dup2 (oldfd, 1); / / printf ("redirected successfully\ n");} close (oldfd); return 0;}
The result of the run is to output the words "redirected successfully" in a.txt.
So now let's analyze it according to our own understanding.
A file descriptor corresponds to a file pointer and thus corresponds to a file. First, the file descriptor 1 corresponds to the output device (the default is the terminal), while the newly created
The file descriptor returned by a.txt, we assume that certain conditions are met as 3
So we write it down as:
1-- > terminal
3-- > a.txt
When we use dup, the result
1-- > a.txt
3-- > a.txt
You can see that the dup function is to copy a file descriptor, and the fancy words you understand can also be said to replace the point of one of the file descriptors. 1 originally points to the terminal, but now points to a.txt, so the content originally output to the terminal will be output to a.txt.
This completes the redirect function.
There's a problem.
According to the above code, the redirection finished but did not recover, the file pointer is still pointed to the a.txt in the code, we continue to execute the program, and the output will still be in the a.txt, unless you call exit () or other function to end the process ahead of time.
So what if we want to restore it?
First, we need to define the file descriptor that save_fd uses to hold the terminal we are going to overwrite.
1-- > terminal
3-- > a.txt
4-- > terminal file descriptor 4 is save_fd.
We have made a new file descriptor point to the terminal, so we can rest assured that we can change the direction boldly.
1-- > a.txt
3-- > a.txt
4-- > terminal
After completing the redirection
Plus dup2 (save_fd, 1)
The point of file descriptor 1 becomes the point of save_fd, that is, it is restored to the original point to the terminal, so you can continue to output under the terminal.
1-- > terminal
3-- > a.txt
Then we turn off the temporary "memory".
Close (save_fd)
This completes the redirection plus recovery.
However, in practice, I have some minor problems.
Please look at some of the code
Oldfd = open ("a.txt", O_CREAT | O_APPEND | O_RDWR, 0666); / / Open the file
Save_fd = dup (1); / / Save the file descriptor of the terminal
Dup2 (oldfd, 1); / / Redirect to a.txt
Printf ("redirected successfully\ n")
Dup2 (save_fd, 1); / / redirect to the terminal, that is, restore to the terminal
Close (save_fd); / / close
However, when I output, the words "redirected successfully" are not output to a.txt, but are still output to the screen.
I think the code and logic should not be wrong, ah, and finally found a problem for a while, I can write to the a.txt with the write function, but the output with printf can not be output to the a.txt, and finally I added a clean buffer function fflush (stdout) after printf () to solve the problem perfectly, so it is the problem of the buffer, today's focus is not the buffer, and I do not know so much about it.
I did another experiment and put fflush (stdout) in front of printf (), but the output was still wrong.
Then I dare to guess:
When we redirect is complete, the output will be output to a.txt, that is, the specified file, but the printf () function output is the first to arrive at the buffer, we also know that the buffer is of size, so we have to wait for the buffer to fill up before we start to "take" it out. However, in my case, the redirection is restored before we have time to get data from the buffer, so it is still output to the screen. This is where we need to be careful.
Add: dup can also be redirected to network socket file descriptors and pipes
After reading the above, do you have any further understanding of how to redirect and restore the dup and dup2 functions? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.