免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 C/C++ 信号
最近访问板块 发新帖
查看: 1694 | 回复: 4
打印 上一主题 下一主题

信号 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-27 16:20 |只看该作者 |倒序浏览
20可用积分
struct sigaction tSigAct;
void sigCloseHandler (int signo, siginfo_t*, void *)
{
  switch (signo)
  {
    case SIGINT :
    {
      sigReceived = true;
      break;
    }

    case SIGQUIT :
    {
      break;
    }

    case SIGABRT :
    {
      break;
    }

    case SIGPIPE :
    {
      sigReceived = true;
      sigReceivedValue = SIGPIPE;
      break;
    }

    case SIGTERM :
      sigReceived = true;
      break;
    }

    case SIGUSR1 :
    {
      globalClientPointer->parseConfigFile();
      break;
    }
  }
}
  tSigAct.sa_handler = (void(*)(int)) sigCloseHandler;
  sigemptyset( &tSigAct.sa_mask );
  tSigAct.sa_flags   = 0;

   while ( ! sigReceived ){...}

   cout<< sigReceived<<endl;

运行后sigReceived输出true,请问如何知道是收到了什么信号退出的吗?

[ 本帖最后由 dutysmart 于 2009-12-27 17:18 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-12-27 16:35 |只看该作者
是收到了signo信号退出的。。

论坛徽章:
0
3 [报告]
发表于 2009-12-27 17:16 |只看该作者
是,我是问想知道具体哪个的话,可以通过加log看到吗? 还是有其他方法?

原帖由 unistd 于 2009-12-27 16:35 发表
是收到了signo信号退出的。。

论坛徽章:
0
4 [报告]
发表于 2009-12-27 17:38 |只看该作者
打印下呗

  1. printf("signo is %d\n", signo);
复制代码

#define SIGHUP                 1
#define SIGINT                 2
#define SIGQUIT                 3
#define SIGILL                 4
#define SIGTRAP                 5
#define SIGABRT                 6
#define SIGEMT                 7
#define SIGFPE                 8
#define SIGKILL                 9
#define SIGBUS                10
#define SIGSEGV                11
#define SIGSYS                12
#define SIGPIPE                13
...
自己去signal.h里查

或者在你的代码里

  1. case SIGINT :
  2.     {
  3.       sigReceived = true;
  4.       printf("%s\n", "SIGINT");
  5.       break;
  6.     }

  7.     case SIGQUIT :
  8.     {
  9.        printf("%s\n", "SIGQUIT");
  10.       break;
  11.     }

复制代码

论坛徽章:
0
5 [报告]
发表于 2009-12-28 09:41 |只看该作者
static bool sigReceived = false;
struct sigaction tSigAct;
void sigCloseHandler (int signo, siginfo_t*, void *)
{
  switch (signo)
  {
    case SIGINT :
    {
      sigReceived = true;
      cout<<"Received SIGINT signal\n"<<endl;
      break;
    }
    case SIGQUIT :
    {
     cout<<"Received SIGQUIT signal\n"<<endl;  break;
    }
    case SIGABRT :
    {
      break;
    }
    case SIGPIPE :
    {
      sigReceived = true;
      sigReceivedValue = SIGPIPE;
      cout<<"Received SIGABRT signal\n"<<endl;
      break;
    }
    case SIGTERM :
      sigReceived = true;
     cout<<"Received SIGTERM signal\n"<<endl;
      break;
    }
    case SIGUSR1 :
    {
      cout<<"Received SIGUSR1 signal\n"<<endl;
      break;
    }
  }
}
  tSigAct.sa_handler = (void(*)(int)) sigCloseHandler;
  sigemptyset( &tSigAct.sa_mask );
  tSigAct.sa_flags  = 0;

  if ( sigaction( SIGINT, &tSigAct, (struct sigaction *) NULL ) == -1 )
  {  cout < <"Unable to trap SIGINT signal due to error %d\n" < <endl; }
   
  if ( sigaction( SIGQUIT, &tSigAct, (struct sigaction *) NULL ) == -1 )
  { cout < <"Unable to trap SIGQUIT signal due to error %d\n" < <endl;}   

  if ( sigaction( SIGABRT, &tSigAct, (struct sigaction *) NULL ) == -1 )
  {  cout < <"Unable to trap SIGABRT signal due to error %d\n" < <endl;;}
   
  if ( sigaction( SIGTERM, &tSigAct, (struct sigaction *) NULL ) == -1 )
  {  cout < <"Unable to trap SIGTERM signal due to error %d\n" < <endl;}

  if ( sigaction( SIGUSR1, &tSigAct, (struct sigaction *) NULL ) == -1 )
  {  cout < < "Unable to trap SIGUSR1 signal due to error %d\n" < <endl;}

   while ( ! sigReceived ){...}

   cout<< sigReceived<<endl;

当sigReceived为true时候,没有看到任何的信号log的信息。这是什么原因呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP