how to correctly wait for execve to finish?

前端 未结 4 816
甜味超标
甜味超标 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:58

    for this question: "is anything wrong with the above??"

    and regarding this code:

    if (child = fork()) {
        waitpid(child, NULL, 0);
        /*now I know execve is finished*/
        exit(0);
    }
    
    execve(cmd, argv, envp);
    
    1. the fork() function has three kinds of returned value:

      • -1 means an error occurred
      • =0 means fork() was successful and the child process is running
      • >0 means fork() was successful and the parent process is running
    2. the call to execvp() needs to be followed

    (for the rare case of the call failing) with

     perror( "execvp failed" );
     exit( EXIT_FAILURE );
    
    1. the call to fork() returns a pid_t.

    After the call, the code needs to be similar to: (using child as the pid variable)

    if( 0 > child )
    {
        perror( "fork failed");
        exit( EXIT_FAILURE );
    }
    
    else if( 0 == child )
    { // then child process
        execve(cmd, argv, envp);
        perror( "execvp failed" );
        exit( EXIT_FAILURE );
    }
    
    //else
    //{ // else parent process
    waitpid(child, NULL, 0);
    exit( EXIT_SUCCESS );
    

    for your second question, about the error message:

    cc1: error: unrecognized command line option "-mfentry"
    

    the word: unrecognized is mis-spelled, so this is not the actual error message.

    This error message is not related to your question about the changes you made to dash.

    However, the dash does not directly invoke any compile operations, so I suspect the questions are totally unrelated.

    Suggest looking at the makefile for the biosutility utility for why there is a invalid parameter being passed to cc1.

提交回复
热议问题