当输出重定向到文件时,printf()和 system()的结果顺序错误

我有一个 C 程序,它可以编译成一个叫 myprogram 的可执行文件,这是它的主函数:

int main(int argc, char ** argv) {
printf("this is a test message.\n");
system("ls");


return 0;
}

当我在 Linux shell 中运行 myprogram > output.txt,然后检查 output.txt 时,我看到上面列出的 ls的输出“ this is a test message”

我觉得应该反过来。为什么会发生这种情况,我该怎么做才能让“ this is a test message”出现在 output.txt 的顶部?

如果有必要的话,我对 C 语言和命令行都是新手。

6086 次浏览

I suspect it's because of the order in which the stdout buffer gets flushed, which is not necessarily deterministic. It's possible that the parent spawns the ls process and doesn't flush its own stdout until after that returns. It may not actually flush stdout until the process exits.

Try adding fflush (stdout) after the printf statement and see if that forces the output to appear first.

It is related to output buffering. I managed to reproduce the same behaviour. Forcing the flush did it for me.

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char ** argv) {
printf("this is a test message.\n");
fflush(stdout);
system("ls");


return 0;
}

Before adding the fflush:

$ ./main > foo
$ cat foo
main
main.c
this is a test message.

and after:

$ ./main > foo
$ cat foo
this is a test message.
foo
main
main.c

By default output to stdout is line-buffered when connected to a terminal. That is, the buffer is flushed when it's full or when you add a newline.

However, if stdout is not connected to a terminal, like what happens when you redirect the output from your program to a file, then stdout becomes fully buffered. That means the buffer will be flushed and actually written either when it's full or when explicitly flushed (which happens when the program exits).

This means that the output of a separate process started from your code (like what happens when you call system) will most likely be written first, since the buffer of that process will be flushed when that process ends, which is before your own process.

What happens when using redirection (or pipes for that matter):

  1. Your printf call writes to the stdout buffer.
  2. The system function starts a new process, which writes to its own buffer.
  3. When the external process (started by your system call) exits, its buffer is flushed and written. Your own buffer in your own process, isn't touched.
  4. Your own process ends, and your stdout buffer is flushed and written.

To get the output in the "correct" (or at least expected) order, call fflush before calling system, to explicitly flush stdout, or call setbuf before any output to disable buffering completely.