Redirecting of stdout in bash vs writing to file in c with fprintf (speed)

风流意气都作罢 提交于 2019-12-23 09:02:22

问题


I am wondering which option is basically quicker.

What interests me the most is the mechanism of redirection. I suspect the file is opened at the start of the program ./program > file and is closed at the end. Hence every time a program outputs something it should be just written to a file, as simple as it sounds. Is it so? Then I guess both options should be comparable when it comes to speed.

Or maybe it is more complicated process since the operating system has to perform more operations?


回答1:


There is no much difference between that options (except making file as a strict option reduces flexibility of your program). To compare both approaches, let's check, what stays behind a magical entity FILE*:

So in both cases we have a FILE* object, a file descriptor fd - a gateway to an OS kernel and in-kernel infrastructure that provides access to files or user terminals, which should (unless libc has some special initializer for stdout or kernel specially handles files with fd = 1).

How does bash redirection work in compare with fopen()?

When bash redirects file:

fork()                      // new process is created
fd = open("file", ...)      // open new file
close(1)                    // get rid of fd=1 pointing to /dev/pts device
dup2(fd, 1)                 // make fd=1 point to opened file
close(fd)                   // get rid of redundant fd
execve("a")                 // now "a" will have file as its stdout
// in a
stdout = fdopen(1, ...)

When you open file on your own:

fork()                           // new process is created
execve("a")                      // now "a" will have file as its stdout
stdout = fdopen(1, ...)         
my_file = fopen("file", ...)     
    fd = open("file", ...)
    my_file = fdopen(fd, ...)

So as you can see, the main bash difference is twiddling with file descriptors.




回答2:


Yes, you are right. The speed will be identical. The only difference in the two cases is which program opens and closes the file. When you redirect it using shell, it is the shell that opens the file and makes the handle available as stdout to the program. When the program opens the file, well, the program opens the file. After that, the handle is a file handle in both the cases, so there should be absolutely no difference in speed.

As a side remark, the program which writes to stdout can be used in more general ways. You can for example say

./program | ssh remotehost bash -c "cat > file"

which will cause the output of the program to be written to file on remotehost. Of course in this case there is no comparison like one you are making in the question.




回答3:


stdout is a FILE handle, fprintf writes to a file handle, so the speed will be very similar in both cases. In fact printf("Some string") is equivalent to fprintf(stdout, "Some string"). I will say no more :)



来源:https://stackoverflow.com/questions/29700478/redirecting-of-stdout-in-bash-vs-writing-to-file-in-c-with-fprintf-speed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!