0

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

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.

Thanks for any help

4

1 回答 1

0

从手册页:

exec() 函数仅在发生错误时返回。

因此,您的execvp电话可能正在工作,因此它没有返回。

这些exec函数的重点是它们将当前运行的代码替换为另一个程序的代码,因此一旦程序完成运行它就会返回到您的代码是没有意义的。

编辑:

看起来你没有正确调用你的程序。我认为你应该这样称呼它:

char *arg[3] = {"test", (char*) 0};
int value = execvp("/home/ameya/Documents/computer_science/cs170/project1/test", arg);
于 2013-04-21T22:10:44.280 回答