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

Ameya Savale
  • 431
  • 1
  • 8
  • 21

1 Answers1

0

From the man page:

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);
Xymostech
  • 9,710
  • 3
  • 34
  • 44
  • thanks that makes sense and the reason I don't see the "hello world" output is because I haven't redirected the output from my child process, I am assuming? – Ameya Savale Apr 21 '13 at 22:21
  • @AmeyaSavale No, you should be seeing the output... Are you trying to run the `test` program? – Xymostech Apr 21 '13 at 22:53
  • yeah i did and it printed out "hello world", the program only has a main which prints out that line, don't I need to read in the output from the child process using pipes? – Ameya Savale Apr 21 '13 at 22:56
  • @AmeyaSavale I mean, from the terminal, are you trying to run `test /home/ameya/Documents/computer_science/cs170/project1`? If not, what are you running on the terminal? – Xymostech Apr 21 '13 at 22:59
  • once I am in the directory listed by the path above i just run `./test` and it prints out "Hello World" – Ameya Savale Apr 21 '13 at 23:10