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!

 

你可能感兴趣的:(Linux,Kernel)