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

标签 c process execvp

我正在尝试使用 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/

相关文章:

c - 边缘避免程序的逻辑

linux - 查看 linux 中已运行进程的输出

c# - .NET 应用程序如何重定向其自己的标准输入流?

从 char* 数组中的条目创建新字符串

c - 如何使用 execvp——查找文件参数

c - 此代码在 Xcode 上可以运行,但在 leetcode 编译器中失败

c++ - 在 C/C++ 中使用 SDL2_gfx 绘制实心圆

python - multiprocessing Pool的自动杀进程和子进程

c - 如何通过 execvp 将 gdb 调试到子进程中启动的进程?

c - 从 C 中的字符串中提取网址