我正在编写一个程序监视器作为操作系统课程的作业(非常基本,就像它的介绍).
监视器必须做的事情之一是显示它正在监视的程序的终止代码,如果它以"自然原因"或负责终止它的信号代码结束.
现在我只是等待孩子结束执行然后捕获终止代码.这是相关的代码段:
pid_t id = -1;
switch (id = fork()) {
// Error when forking:
case -1:
error(-1, "Something went wrong when forking.");
exit(-1);
// Code for the child process:
case 0:
// Just launch the program we're asked to:
execvp(argv[2], &argv[2]);
// If reached here it wasn't possible to launch the process:
error(1, "Process could not be launched.");
exit(1);
// Code for the parent process:
default:
// Just wait for the child to finish its execution:
wait(&return_value);
} …Run Code Online (Sandbox Code Playgroud)