繁体   English   中英

为什么不执行此printf语句

[英]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;
}

如果未显示printf语句,则else下方的所有printf语句,

使用的编译器:gcc版本6.3.1 20170306(GCC)

作业系统:基于Linux,

我的假设:stderr用自己的数据替换当前行的输出,但是只要输入在自己的行中,例如printf("This Line is Printed\\n") ,我就可以看到输出。 但是,如果我以这种方式写出printf("This Line isn't Printed")

要么

难道只发生在我身上吗? 或OS出现一些渲染问题。

我上面的程序的输出:

产量

尝试使用fflush(NULL); execvp(argv[1], argv);

从男人起:

对于输出流,fflush()强制通过给定的输出或更新流,通过该流的底层写函数写入所有用户空间缓冲的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM