问题
I get an output of "hi!". Why is this not also printing "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;
}
I know I'm not creating a child process first. If I take out the execvp line, it works as expected. Weird. (Note: "echo" refers to https://en.wikipedia.org/wiki/Echo_(command))
回答1:
The string is in the io buffer - so pull the chain and flush that buffer
i.e. add
fflush(stdout)
after the printf
(or add \n
to the printf
)
来源:https://stackoverflow.com/questions/32547540/why-is-printf-before-exevp-not-running