我得到了一个“hi!”的输出。为什么它没有打印出“something”?
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv) {
char* program_name = "echo";
char* args[]= {program_name,"hi!",NULL};
printf("something");
execvp(program_name,args);
return 0;
}
我知道我没有首先创建子进程。如果我去掉execvp行,它会按预期工作。奇怪的事情。(注意:"echo"指的是https://en.wikipedia.org/wiki/Echo_(command))
printf
确实在运行,只是它不会刷新缓冲区,因为stdout
的标准缓冲模式是行缓冲,所以只有在出现换行符时才会自动刷新。尽管如此,@andrew_ghobrial,它回答了你的问题,建议接受它。 - user2371524\n
是不够的;您必须使用fflush(3)
显式刷新缓冲区。 - Filipe Gonçalves