#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
int main( int argc, char *argv[] ) {
printf("Hello, to the Simulation world,\n");
int pid = fork();
if(pid < 0) {
fprintf(stderr,"Oops something went wrong\n");
}else if (pid == 0){
printf("I'm child, and I do all the work, \n");
printf("This isn't printed");
// Any printf statement over here isn't executed \\
execvp(argv[1], argv);
} else {
wait(&pid);
printf("I'm Parent and I do nothing, but I will wait till my child dies.\n");
}
return 0;
}
如果未显示 printf 语句,则 else 下面的任何 printf 语句,
使用的编译器:gcc version 6.3.1 20170306 (GCC)
操作系统:基于Linux,
我的假设:stderr 正在用它自己的数据替换当前行输出,但是每当输入在它自己的行中时printf("This Line is Printed\n")
,我就可以看到输出。但是如果我这样写printf("This Line isn't Printed")
。
或者
它只发生在我身上吗?或者操作系统的一些渲染问题。
我上面程序的输出:
fflush(NULL);
之前尝试使用execvp(argv[1], argv);
来自 man fflush :
对于输出流, fflush() 通过流的底层写入函数强制写入给定输出或更新流的所有用户空间缓冲数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句