Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...