#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,
Compiler used: gcc version 6.3.1 20170306 (GCC)
OS: Linux based,
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. But if I write it in this way printf("This Line isn't Printed")
.
or
Is it happening only for me? Or some rendering problem with OS.
Output for my above program:
Try using fflush(NULL);
before 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.