Does the C execv() function terminate the child proccess?

Chris Allen picture Chris Allen · Mar 2, 2011 · Viewed 9.6k times · Source

Heres a breakdown of my code.

I have a program that forks a child (and registers the child's pid in a file) and then does its own thing. The child becomes any program the programmer has dignified with argv. When the child is finished executing, it sends a signal (using SIGUSR1) back to the parent processes so the parent knows to remove the child from the file. The parent should stop a second, acknowledge the deleted entry by updating its table, and continue where it left off.

pid = fork();
switch(pid){
case -1:{
    exit(1);
}

case 0 :{
    (*table[numP-1]).pid = getpid(); //Global that stores pids
    add();                             //saves table into a text file 
    freeT(table);                  //Frees table 
    execv(argv[3], &argv[4]);          //Executes new program with argv 
    printf("finished execution\n");  
    del(getpid());                     //Erases pid from file 
    refreshReq();                      //Sends SIGUSR1 to parent
    return 0;
}
default:{
    ... //Does its own thing
}
}

The problem is that the after execv successfully starts and finishes (A printf statement before the return 0 lets me know), I do not see the rest of the commands in the switch statement being executed. I am wondering if the execv has like a ^C command in it which kills the child when it finishes and thus never finishes the rest of the commands. I looked into the man pages but did not find anything useful on the subject.

Thanks!

Answer

Steve Jessop picture Steve Jessop · Mar 2, 2011

execv replaces the currently executing program with a different one. It doesn't restore the old program once that new program is done, hence it's documented "on success, execv does not return".

So, you should see your message "finished execution" if and only if execv fails.