2009年2月2日星期一

SIGUSER1 通过kill -10 pid进行触发的例子

#include <signal.h>

#include <stdio.h>

#include <iostream.h>

#filename: 1. C

static void sig_usr(int);

int main(void)

{

    cout<<"SIGUSR1:"<<SIGUSR1<<endl;    

    if(signal(SIGUSR1,sig_usr) == SIG_ERR)

      cout<<"can't catch SIGUSR1 \n"<<endl;

    if(signal(SIGUSR2,sig_usr) == SIG_ERR)

      cout<<"can't catch SIGUSR2"<<endl;

    if(signal(SIGBUS,sig_usr) == SIG_ERR)

      cout<<"can't catch SIGBUS"<<endl;

    for(;;)

      pause();

}

static void sig_usr(int signo)

{

    if(signo == SIGUSR1)

       cout<<"received SIGUSR1!"<<endl;

    else if(signo == SIGUSR2)

       cout<<"received SIGUSR2!"<<endl;

    else

       cout<<"received signal:"<<signo<<endl;

    return;

}

编译该程序: $: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 ..... */

}

From: http://blog.csdn.net/lllxy/archive/2008/12/24/3597435.aspx

0 评论: