gpt4 book ai didi

c - exevp 跳过所有代码,直到 c 中的 wait 调用

转载 作者:行者123 更新时间:2023-11-30 17:52:30 26 4
gpt4 key购买 nike

我正在尝试使用 forkexecvp 执行文件,但是遇到了一些错误。我还没有找到任何解决方案来解决我在网上遇到的问题,因为我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com