timer使用

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      

struct inn
{
    int a;
    int b;
};

void timer_func(union sigval lo_val)
{
    printf("enter fun\r\n");
    printf("a= %d\r\n", ((struct inn *)(lo_val.sival_ptr))->a);
   printf("b= %d\r\n", ((struct inn *)(lo_val.sival_ptr))->b);

    return;
}

int main(int argc, char *argv[])
{
    timer_t my_timer;
    struct sigevent my_sigev;
    struct itimerspec old_spec;
    struct itimerspec new_spec;

    struct inn inst;

    int Ret;

    printf("enter\n");

    inst.a = 3;
    inst.b = 4;
    my_sigev.sigev_notify = SIGEV_THREAD;
    my_sigev.sigev_signo = SIGALRM;
    my_sigev.sigev_value.sival_ptr = (void *)&inst; /*传递的参数*/
    my_sigev.sigev_notify_function = timer_func;
    my_sigev.sigev_notify_attributes = NULL;

    Ret = timer_create(CLOCK_MONOTONIC, &my_sigev, &my_timer);
    printf("timer = %d\n", (int)my_timer);
    if(0 != Ret)
    {
        printf("timer create error\r\n");
        return (-1);
    }

    new_spec.it_interval.tv_sec = 5;
    new_spec.it_interval.tv_nsec = 0;
    new_spec.it_value.tv_sec = 5;
    new_spec.it_value.tv_nsec = 0;
    Ret = timer_settime(my_timer, 0, &new_spec, &old_spec);
    printf("Ret = %d\r\n", Ret);
    printf(" %d\n", old_spec.it_interval.tv_sec);
    printf("%d\n", old_spec.it_value.tv_sec);

    if(0 != Ret)
    {
        printf("timer set1 error\r\n");
        return (-1);
    }

    sleep(3);
    Ret = timer_gettime(my_timer, &old_spec);
    printf("%d\n", old_spec.it_interval.tv_sec);
    printf("%d\n", old_spec.it_value.tv_sec);

    pause();
    return 0;
}    

     
     
    
    
   
   

编译
[root@localhost exam]# gcc -o my_timer my_timer.c -lrt
[root@localhost exam]# ./my_timer 
enter
timer = 135045272
Ret = 0
 0
0
5
1
enter fun
a= 3
b= 4
enter fun
a= 3
b= 4
enter fun
a= 3
b= 4
enter fun
a= 3
b= 4
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值