小编Kri*_* H.的帖子

execvp/fork - 如何捕获不成功的执行?

现在我正在编写一个必须执行子进程的C程序.我没有同时或任何事情做多个子进程,所以这是相当简单的.我肯定会成功执行内置shell程序(例如cat和echo之类的东西),但我还需要能够告诉其中一个程序何时无法成功执行.我正在尝试使用以下简化代码:

int returnStatus; // The return status of the child process.
pid_t pid = fork();

if (pid == -1) // error with forking.
{
   // Not really important for this question.
}
else if (pid == 0) // We're in the child process.
{
   execvp(programName, programNameAndCommandsArray); // vars declared above fork().

   // If this code executes the execution has failed.
   exit(127); // This exit code was taken from a exec tutorial -- why 127?
}
else // We're in the …
Run Code Online (Sandbox Code Playgroud)

c fork exec execvp

5
推荐指数
1
解决办法
6098
查看次数

标签 统计

c ×1

exec ×1

execvp ×1

fork ×1