execvp/fork — how to catch unsuccessful executions?

前端 未结 3 969
臣服心动
臣服心动 2021-02-02 04:18

Right now I\'m writing a C program that must execute a child process. I\'m not doing multiple child processes simultaneously or anything, so this is fairly straightforward. I am

相关标签:
3条回答
  • 2021-02-02 04:38

    wait(2) gives you more than just the exit status of the child process. In order to get the real exit status, you need to use the WIFEXITED() macro to test if the child exited normally (as opposed to abnormally via a signal etc.), and then use the WEXITSTATUS() macro to get the real exit status:

    wait(&status);
    if(WIFEXITED(status))
    {
        if(WEXITSTATUS(status) == 0)
        {
            // Program succeeded
        }
        else
        {
            // Program failed but exited normally
        }
    }
    else
    {
        // Program exited abnormally
    }
    

    In order for execvp(3) to run a program in the current directory, you either need to add the current directory to your $PATH environment (generally not a good idea), or pass it the full path, e.g. use ./myprogram instead of just myprogram.

    0 讨论(0)
  • 2021-02-02 04:39

    In terms of failure detection, if an exec() function replaces the current process with a new one, then the current process is gone; it doesn't matter if the executed program decides that what it has been asked to do is a success or failure. However, the parent process from before the fork can discover the child's exit code which would likely have the command success/failure information.

    In terms of finding executables, execvp() duplicates the action of the shell, searching the current path. If it is not finding executables in the working directory, it is likely that directory is not in the search path (or the files are not actually executable). You can try specifying them by a full path name.

    If you simply want to run a command and wait for the result, you might want to use the system() function which handles this for you, instead of building it yourself with fork/exec.

    0 讨论(0)
  • 2021-02-02 04:59

    This is a classic problem with a very elegant solution. Before forking, create a pipe in the parent. After fork, the parent should close the writing end of the pipe, and block attempting to read from the reading end. The child should close the reading end and set the close-on-exec flag, using fcntl, for the writing end.

    Now, if the child calls execvp successfully, the writing end of the pipe will be closed with no data, and read in the parent will return 0. If execvp fails in the child, write the error code to the pipe, and read in the parent will return nonzero, having read the error code for the parent to handle.

    0 讨论(0)
提交回复
热议问题