我正在尝试使用 fork
和 execvp
执行文件,但是遇到了一些错误。我还没有找到任何解决方案来解决我在网上遇到的问题,因为我的 exevp 没有收到任何错误,它也没有运行。这是我的代码:
pid_t child;
int status;
child = fork();
char *arg[3] = {"test","/home/ameya/Documents/computer_science/cs170/project1", (char*) 0};
if(child == 0){
printf("IN CHILD BEFORE EXECVP\n");
int value = execvp(arg[0],arg);
if(value < 0){
printf("ERROR\n");
}else{
printf("In Child : %i\n", value);
}
}
if(waitpid(child, &status, 0) != child){
printf("ERROR IN PROCESS\n");
}
printf("In Parent\n");
当我尝试运行此代码时,它仅输出“IN CHILD BEFORE EXCEPTION”和“IN PARENT”,它不会打印出中间的任何 printf 语句,为什么要这样做。我正在尝试运行一个简单的可执行文件,该文件将“hello world”打印到标准输出。
感谢您的帮助
最佳答案
来自手册页:
The exec() functions only return if an error has occurred.
So, your execvp
call is presumably working, and thus it is not returning.
The point of the exec
functions is that they replace the currently running code with the code of another program, so it doesn't make sense that it would then return to your code once the program was done running.
Edit:
It looks like you're not calling your program correctly. I think you should be calling it like this:
char *arg[3] = {"test", (char*) 0};
int value = execvp("/home/ameya/Documents/computer_science/cs170/project1/test", arg);
关于c - exevp 跳过所有代码,直到 c 中的 wait 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16136725/