我是LINUX C编程新手,我的任务是编写一个关于进程的程序。如何在父进程中捕捉术语动作信号?
我需要处理两个进程,父进程和子进程。
我的目标是让父叉进程(子进程),然后子进程执行可能会终止的程序失败。父进程等待子进程终止,并获取从子信号发起的信号,如中止或分段错误。
但是,我遇到一些问题。
我发现“Core Action”信号可以很容易的被检测到,但是“Term action”无法被检测到!
无法检测到“术语操作”信号,例如SIGALRM(14)或SIGINT(2)。 它似乎被归类为终止成功。
这里是我的代码:
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <cstring>
#include <errno.h>
using namespace std;
bool check = true;
void mySignal(int sig){
int status;
pid_t childPid = wait(&status) ;
if(WIFEXITED(status)){
printf("The child is terminated success!!\n");
}
else{
if(WIFSIGNALED(status)){
int termsig = WTERMSIG(status) ;
printf("termsig = %d %d\n",status, termsig) ;
}
}
check = false ;
}
int main(int argc , char *argv[]){
signal(SIGCHLD, mySignal) ;
pid_t pid = fork() ;
if(pid < 0){
printf("fork error\n");
exit(-1) ;
}
else if(pid == 0){
execl(argv[1], NULL);
exit(0) ;
}
while(check) ;
return 0 ;
}
有谁知道如何解决这个问题?
哦!感谢您的提醒!我会考虑这个问题:) – ChihMin 2014-10-10 08:33:57
@ChihMin不要忘记验证一个答案,如果有人解决了你的问题。 – Mekap 2015-04-28 08:46:33