exevp跳过所有代码,直到在c中等待调用 [英] exevp skips over all code until wait call in c

查看:132
本文介绍了exevp跳过所有代码,直到在c中等待调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用forkexecvp执行文件,但是遇到一些错误.我没有找到解决此在线问题的任何解决方案,因为我没有从exevp中得到任何错误,也没有运行它.这是我的代码:

I am trying to execute a file using fork and execvp, however I am encountering some errors. I have not found any solutions to the problem I am having here online, since I don't get any errors from my exevp nor does it run. Here is my code:

 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");

当我尝试运行此代码时,它仅输出未成年子女"和成父母",但在此之间,它不会打印出任何printf语句.我正在尝试运行一个简单的可执行文件,该文件将"hello world"打印到stdout.

When I try to run this code it only outputs the "IN CHILD BEFORE EXCEPTION" and "IN PARENT" it doesn't print out any of the printf statements in between why does it do that. The file I am trying to run a simple executable that prints "hello world" to stdout.

感谢您的帮助

推荐答案

从手册页开始:

The exec() functions only return if an error has occurred.

因此,您的execvp调用可能正在运行,因此没有返回.

So, your execvp call is presumably working, and thus it is not returning.

exec函数的要点是它们将当前正在运行的代码替换为另一个程序的代码,因此,一旦程序运行完毕,它将返回您的代码是没有意义的.

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.

您似乎没有正确调用程序.我认为您应该这样称呼它:

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);

这篇关于exevp跳过所有代码,直到在c中等待调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆