how to correctly wait for execve to finish?

前端 未结 4 779
甜味超标
甜味超标 2021-01-14 15:31

A C source code (compiled and running Linux Centos 6.3) has the line:

execve(cmd, argv, envp);

execve does not return, but I w

4条回答
  •  迷失自我
    2021-01-14 15:51

    Following Rici's excellent comments and answer, I found the root cause of the problem.

    The original code exits with whatever cmd exited. I changed that to exit with 0 always. That is why the code behaves differently.

    The following fix does not exhibit the error:

    int status;
    
    if (child = fork()) {
            waitpid(child, &status, 0);
            /*now we know execve is finished*/
            if (WIFEXITED(status))
                exit(WEXITSTATUS(status));
            exit(1);
        }
    
    execve(cmd, argv, envp);
    

提交回复
热议问题