I’m playing with execvp(), and found a interesting thing, here is the code first of all.
using namespace std;
#include <iostream>
#include <unistd.h>
int main(){
char *argv[3];
int pid = fork();
if (pid == 0){
argv[0] = "ls";
argv[1] = "-l";
argv[2] = NULL;
execvp("ls", argv);
}
}
This is a simple fork + execvp problem, but I found that after “ls” was executed successfully, I will have to hit Enter to come back to command line (shell).
Anyone know how do I make it so that after execvp(“ls”) I can go back to shell without hit my “Enter”?
Your main program exits before the
lscompletes. By the timelscompletes, its output has obscured the shell prompt.You can solve this with either of these:
else wait(0);after theif‘s closing brace.fork().