2

下面提到的是我的 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
4

2 回答 2

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()在你的处理程序中调用——那是不安全的。您的“标志”布尔值可能应该是volatile sig_atomic_t类型。

于 2013-09-12T14:58:04.063 回答
1

您的问题是,一旦处理了信号,流程就会返回到有问题的代码,这又会产生分段错误。

解决这个问题的更简单方法是让你的信号处理程序kill()对孩子进行 ing,然后exit()abort()

或者,siglongjmp()或者将上下文切换到安全的地方(如this answer所示,尽管我建议您使用makecontext()而不是处理特定于平台的寄存器。)。

于 2013-09-12T13:48:14.653 回答