linux中的TIMER使用入门

#include  < sys / time.h >
#include 
< signal.h >
#include 
< unistd.h >
#include 
< stdio.h >

#define  MAXSEC 1
#define  MAXUSEC 0

void   real_handle()
{
    printf(
" this is real_handl " );
}

long  unsigned  int  fibonacci(unsigned  int  n)

{

      
if (n == 0 )

              
return   0 ;

        
else   if (n == 1   ||  n == 2 )

                
return   1 ;

          
else

                  
return  (fibonacci(n - 1 +  fibonacci(n - 2 ));

}

static   struct  itimerval p_realt;

main()
{
    p_realt.it_interval.tv_sec 
=  MAXSEC;
    p_realt.it_interval.tv_usec 
=  MAXUSEC;
    p_realt.it_value.tv_sec 
=  MAXSEC;
    p_realt.it_value.tv_usec 
=  MAXUSEC;

    signal(SIGALRM,real_handle);

    
if (setitimer(ITIMER_REAL, & p_realt,( struct  itimerval  * ) 0 ==   - 1 )
        perror(
" setitimer error! " );
    fibonacci(
40 );
}

/
THE OUTPUT IS:
[root@localhost Lab03]# ./a.out
this is real_handl
this is real_handl
this is real_handl

EXPLAINATION:
the value of p_realt is given to ITIMER_REAL,a timer.When this timer expired,signal SIGALRM will be send out and function real_handle() will be invoked.
if it_interval is set too big,like 100 sec,real_handl() won't be invoked.because SIGALRM is send out every 100 seconds.
if it_interval is set too small,like 1000 usec,real_handle() will be invoked too frequently!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值