execvp/fork — how to catch unsuccessful executions?

前端 未结 3 973
臣服心动
臣服心动 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: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.

提交回复
热议问题