[英]Why isn't this printf statement executed
#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;
}
Any printf statement below the else if printf statement isn't shown, 如果未显示printf语句,则else下方的所有printf语句,
Compiler used: gcc version 6.3.1 20170306 (GCC) 使用的编译器:gcc版本6.3.1 20170306(GCC)
OS: Linux based, 作业系统:基于Linux,
My assumption: stderr is replacing current line output with its own data, but whenever input is in its own line like, printf("This Line is Printed\\n")
, I can see the output. 我的假设:stderr用自己的数据替换当前行的输出,但是只要输入在自己的行中,例如printf("This Line is Printed\\n")
,我就可以看到输出。 But if I write it in this way printf("This Line isn't Printed")
. 但是,如果我以这种方式写出printf("This Line isn't Printed")
。
or 要么
Is it happening only for me? 难道只发生在我身上吗? Or some rendering problem with OS. 或OS出现一些渲染问题。
Output for my above program: 我上面的程序的输出:
Try using fflush(NULL);
尝试使用fflush(NULL);
before execvp(argv[1], argv);
在execvp(argv[1], argv);
From man fflush : 从男人起:
For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. 对于输出流,fflush()强制通过给定的输出或更新流,通过该流的底层写函数写入所有用户空间缓冲的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.