#include <signal.h>
#include <unistd.h>
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