下面提到的是我的 C++ 程序。我想杀死所有子进程和父进程,当任何子进程或父进程内部生成分段错误信号时。
我从 linux 内核 sigterm 信号正在达到 handle_signal 函数。但是我被困在后面的流程中,当调试时我看到甚至设置了 gSegSignalRcvd=1 。但是进程控制没有到达 child r parent 中的任何 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