//sigprof.c
/*************************************************************
FileName : sigprof.c
FileFunc : 2秒产生一次信号处理
Version : V0.1
Author : Sunrier
Date : 2012-05-25
Descp : Linux下信号处理
*************************************************************/
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
void sig_prof(int signo)
{
printf("Signal is caught ! Signal number is %d !\n",signo);
//system("./sig");
}
void init_sigaction( void )
{
struct sigaction act;
act.sa_handler=sig_prof;
act.sa_flags=0;
sigemptyset(&act.sa_mask);//初始化信号集
sigaction(SIGPROF,&act,NULL);//SIGPROF表示使用setitimer指定的Profiling Interval Timer所产生
}
void init_time( void )
{
struct itimerval value;
value.it_value.tv_sec=2;//秒
value.it_value.tv_usec=0;//微秒
value.it_interval=value.it_value;//间隔时间,如果不指定就只实现一次定时,如果it_value和it_interval两者都清零,则会清除定时器。
setitimer(ITIMER_PROF,&value,NULL);//ITIMER_PROF送出SIGPROF信号
}
int main(int argc,char *argv[])
{
init_sigaction();//初始化设置信号处理
init_time();//精确定时
while(1);
return 0;
//exit(0);
}
[Sunrier@localhost Sig]$ gcc -o sig sigprof.c
[Sunrier@localhost Sig]$ ./sig
Signal is caught ! Signal number is 27 !
Signal is caught ! Signal number is 27 !
Signal is caught ! Signal number is 27 !
Signal is caught ! Signal number is 27 !
Signal is caught ! Signal number is 27 !
Signal is caught ! Signal number is 27 !
.......................................................................
.......................................................................
.......................................................................
//sigalrm.c
/*************************************************************
FileName : sigalrm.c
FileFunc : 2秒产生一次信号处理
Version : V0.1
Author : Sunrier
Date : 2012-05-25
Descp : Linux下信号处理
*************************************************************/
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
void sig_prof(int signo)
{
printf("Signal is caught ! Signal number is %d !\n",signo);
//system("./sig");
}
void init_sigaction( void )
{
struct sigaction act;
act.sa_handler=sig_prof;
act.sa_flags=0;
sigemptyset(&act.sa_mask);//初始化信号集
//sigaction(SIGPROF,&act,NULL);//SIGPROF表示使用setitimer指定的Profiling Interval Timer所产生
sigaction(SIGALRM,&act,NULL);//用alarm函数设置的timer超时或setitimer函数设置的interval timer超时
}
void init_time( void )
{
struct itimerval value;
value.it_value.tv_sec=2;//秒
value.it_value.tv_usec=0;//微秒
value.it_interval=value.it_value;//间隔时间,如果不指定就只实现一次定时,如果it_value和it_interval两者都清零,则会清除定时器。
//setitimer(ITIMER_PROF,&value,NULL);//ITIMER_PROF送出SIGPROF信号
setitimer(ITIMER_REAL,&value,NULL);//ITIMER_REAL送出SIGALRM信号
}
int main(int argc,char *argv[])
{
init_sigaction();//初始化设置信号处理
init_time();//精确定时
while(1);
return 0;
//exit(0);
}
[Sunrier@localhost Sig]$ gcc -o sig sigalrm.c
[Sunrier@localhost Sig]$ ./sig
Signal is caught ! Signal number is 14 !
Signal is caught ! Signal number is 14 !
Signal is caught ! Signal number is 14 !
Signal is caught ! Signal number is 14 !
Signal is caught ! Signal number is 14 !
Signal is caught ! Signal number is 14 !
.......................................................................
.......................................................................
.......................................................................