下面提到的是我的C ++程序。我想要杀死所有的儿童和儿童父进程,当在子进程或父进程内部生成分段错误信号时。
我从linux内核sigterm信号到达handle_signal函数。 但我在以后的流程中坚持,当调试时我甚至看到gSegSignalRcvd = 1被设置。 但是进程控制没有到达子r父级中的任何for循环。
#include<iostream>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<signal.h>
bool gSignalRcvd = 0;
bool gSegSignalRcvd = 0;
pid_t lCid1;
pid_t lCid2;
pid_t lPid;
void handle_signal(int sig)
{
switch(sig)
{
case SIGSEGV:
{
gSegSignalRcvd = 1;
printf("Reecived Signal:%d SegSignal:%d\n", gSignalRcvd, gSegSignalRcvd);
}
default:
printf("Reecived Signal :%d\n",sig);
};
gSignalRcvd = 1;
signal(SIGSEGV, handle_signal);
}
int main()
{
signal(SIGSEGV, handle_signal);
lCid1 = fork();
if(lCid1 < 0)
{
printf("Error in child Creation Err:%d\n", lCid1);
exit(1);
}
else if(lCid1 > 0)
{
lCid2 = fork();
if(lCid2 < 0)
{
printf("Error in child Creation Err:%d\n", lCid1);
exit(2);
}
else if(lCid2 > 0)
{
struct tm * lTimePtr;
int lSleepTime=0;
int mUploadTime=0;
int lCount = 1;
time_t lTimeObj;
for(;;)
{
lCount = lCount++;
printf("Received Signal @ Parent Seg:%d Sig%d\n", gSegSignalRcvd, gSignalRcvd);
if(gSignalRcvd)
{
if(gSegSignalRcvd)
{
printf("Received Signal Inside Parent now killing the Process\n");
kill(lCid1, SIGTERM);
kill(lCid2, SIGTERM);
kill(getpid(), SIGTERM);
}
}
gSignalRcvd = 0;
printf("Time:%ld tm_min:%d tm_sec:%d UploadTime:%d SleepTime:%d\n", lTimeObj, lTimePtr->tm_min, lTimePtr->tm_sec, mUploadTime, lSleepTime);
sleep(mUploadTime + lCount);
}
}
else
{
int lNumb,lResult;
for(;;)
{
printf("Received Signal @ Child2 Seg:%d Sig%d\n", gSegSignalRcvd, gSignalRcvd);
if(gSignalRcvd)
{
if(gSegSignalRcvd)
{
printf("Received Signal Inside Child2 **************\n");
kill(lCid1, SIGTERM);
kill(lCid2, SIGTERM);
kill(getpid(), SIGTERM);
}
}
gSignalRcvd = 0;
printf("Printing the Child2 Value\n");
lNumb = rand() / 100;
lResult = lNumb / 2;
printf("Number:%s Result:%d\n", lNumb, lResult);
usleep(lNumb);
}
}
}
else
{
for(;;)
{
printf("Received Signal @ Child1 Seg:%d Sig%d\n", gSegSignalRcvd, gSignalRcvd);
if(gSignalRcvd)
{
if(gSegSignalRcvd)
{
printf("Received Signal Inside Child1 ---------------\n");
kill(lCid1, SIGTERM);
kill(lCid2, SIGTERM);
kill(getpid(), SIGTERM);
}
}
gSignalRcvd = 0;
printf("Printing the Child1 Value\n");
usleep(rand());
}
}
return 0;
}
----------- Program OutPut ---------
[scuser@Bilx-Congo-Lab ~]$ ./sigHandle
Received Signal @ Child1 Seg:0 Sig0
Printing the Child1 Value
Received Signal @ Child2 Seg:0 Sig0
Printing the Child2 Value
Number:Reecived Signal:0 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
Reecived Signal :11
Reecived Signal:1 SegSignal:1
答案 0 :(得分:1)
您的问题是,一旦信号被处理,流程将返回到有问题的代码,再次产生分段错误。
更简单的方法是让信号处理程序执行子项的kill()
,然后exit()
或abort()
。
或者,siglongjmp()
或将上下文切换到安全位置(如this answer所示,但我建议您使用makecontext()
而不是处理特定于平台的寄存器。)。
答案 1 :(得分:1)
将所有进程放在他们自己的进程组中,然后在任何成员获得SEGV时将SIGTERM发送给整个组。
在父母中,在任何分叉之前:
pid_t gPGid;
volatile sig_atomic_t gSegSignalRcvd = 0;
... main() ...
setpgid(0, 0); // Make new process group, if needed
gPGid = getpgid(0);
然后,在处理程序中,发出整个进程组的信号:
void handler(int sig) {
if (sig == SIGSEGV) {
kill(-gPGid, SIGTERM); // Note the -gPGid here
_exit(1); // We segfaulted - we'd better quit now
} else if (sig == SIGTERM) {
gSegSignalRcvd = 1;
}
...
}
一些评论:不要使用signal()
以及在处理程序中重新安装处理程序的日期约定 - 请改用sigaction()
。不要在处理程序中调用printf()
- that's not safe。你的“旗帜”布尔可能应该是volatile sig_atomic_t
种类型。