SIGUSER1 通过kill -10 pid进行触发的例子,并且该信号触发了与Accept函数。

  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <iostream.h>
  4. #filename: 1. C
  5. static void sig_usr(int);
  6. int main(void)
  7. {
  8.     cout<<"SIGUSR1:"<<SIGUSR1<<endl;    
  9.     if(signal(SIGUSR1,sig_usr) == SIG_ERR)
  10.       cout<<"can't catch SIGUSR1 /n"<<endl;
  11.     if(signal(SIGUSR2,sig_usr) == SIG_ERR)
  12.       cout<<"can't catch SIGUSR2"<<endl;
  13.     if(signal(SIGBUS,sig_usr) == SIG_ERR)
  14.       cout<<"can't catch SIGBUS"<<endl;
  15.     for(;;)
  16.       pause();
  17. }
  18. static void sig_usr(int signo)
  19. {
  20.     if(signo == SIGUSR1)
  21.        cout<<"received SIGUSR1!"<<endl;
  22.     else if(signo == SIGUSR2)
  23.        cout<<"received SIGUSR2!"<<endl;
  24.     else
  25.        cout<<"received signal:"<<signo<<endl;
  26.     return;
  27. }

编译该程序: $:g++ -o test1 1.C

得到可执行文件test1,然后再后台运行1

$: test1 &

[1] 5201

 

然后向进程5201触发SIGUSR1信号:

$: kill -10 5201

$:received SIGUSR1!   

 

并且SIGUSER2的值为:12,通过kill -12 5201,可以触发SIGUSER2信号。

 

为了解决当触发SIGUSER1信号误触发Accept,对于Accept的调用应该按照如下方式:

while(1)
{
   int nRet = accept(....);
   if ( nRet == -1 )
   {      
      if (errno == EINTR)
         continue;
      else
         return ;
  }
   /* do something ..... */
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值