c实现signal函数实例

#include 
#include 


void sig_hander(int signum)
{
    printf("catch the signal %d\n",signum);
    return ;
}


int main(int argc,char **argv)
{
    pid_t pid;
    pid = getpid();
    printf("pid[%d]\n",getpid());
    if(signal(SIGINT,sig_hander)==SIG_ERR)
    {
        printf("catch error!\n");
        return -1;
    }
    while(1);
    return 0;
}

执行:

momo@momo:mydev/cc $ a.out
pid[8675]
^Ccatch the signal 2
^Ccatch the signal 2
^Ccatch the signal 2


你可能感兴趣的:(C&C++)