So I know printf()
is higher level than write()
and ends up using write()
. Printf()
is buffered and write()
makes system calls.
Example 1, if I were to run a program with printf()
before write()
then it would output the value of printf()
before the value of write()
.
Example 2, if I were to run the same program and have it go through output redirection into a file, the value of write()
outputs before printf()
.
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("This is a printf test
");
write(STDOUT_FILENO, "This is a write test
", 21);
return 0;
}
I don't understand what is happening here. In example 1, is the program waiting for printf()
s output before running write()
? In example 2, is the program redirecting the first output that is ready? And because write()
is lower level, and does not need to buffer like printf()
then it is printed first?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…