我有一个非常奇怪的错误,我不知道为什么会这样,我正在尝试创建一个shell,如果我想调用多个execvp,它将无法正常工作。如果我只是传递echo hello,它会起作用,但是如果我为第一个execvp提供了echo hello,为第二个execvp提供了echo hello world,则第二个execvp将显示“ hello echo hello world”。
void testexecute(char** arglist){
int k;
int status=0;
pid_t child_pid;
child_pid=fork();
cout<<"This is our child_pid number "<<child_pid<<endl;
if (child_pid == -1){
// error, failed to fork()
}
else if (child_pid > 0){
int status;
waitpid(child_pid, &status, 0);
cout<<"Our status here be "<<status<<endl;
return status;
}
else {
// we are the child
if(execvp(arglist[0],arglist)==-1)
{
_exit(EXIT_FAILURE); // exec never returns
}
else
{
cout<<"Second else has reached "<<endl;
exit(0);
}
}
}
int main() {
char *argv[] = {"echo","testing","these","functions"};
char *argvlist2[]= {"echo"," testssss","1","2"};
testexecute(argvlist2);
testexecute(argv);
return 0;
}
运行此代码时,这是我的输出
testssss 1 2
testing these functions echo testssss 1 2
我尝试检查testexecute内部参数的内容,并将其传递给正确的参数,所以我不知道为什么它还将前一个参数传递给execvp()。