Chinaunix首页 | 论坛 | 博客
  • 博客访问: 735112
  • 博文数量: 119
  • 博客积分: 137
  • 博客等级: 少校
  • 技术积分: 1582
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-28 16:39
文章分类

全部博文(119)

文章存档

2017年(3)

2016年(7)

2014年(1)

2013年(8)

2012年(20)

2011年(27)

2010年(53)

分类: LINUX

2010-07-02 09:37:38

#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!


 

阅读(941) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~