Linux C 时间和定时器

当前时间

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

int main()
{
    //<time.h> 精度s
    time_t now = time(NULL);
    printf("current time: %lds\n", now);

    //<sys/time.h> 精度us
    struct timeval tval;
    gettimeofday(&tval, NULL);
    printf("current time: %lds %ldus\n", tval.tv_sec, tval.tv_usec);

    //<time.h> 精度ns
    struct timespec res;
    clock_gettime(CLOCK_REALTIME, &res);
    printf("current time: %lds %ldns\n", res.tv_sec, res.tv_nsec);

    //<time.h> 讲time_t转换为struct tm
    struct tm* t = localtime(&now);
    printf("current time: %d/%d/%d %d:%d:%ds\n",
        t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
        t->tm_hour, t->tm_min, t->tm_sec);
}

定时器

alarm(int seconds)

  • seconds秒后触发 一次 SIGALRM信号
#include <stdio.h>

#include <signal.h>
#include <unistd.h>

void handle_alarm(int sig)      //sig is 14(SIGALRM)
{
    psignal(sig, NULL);     //will print: Alarm clock
    printf("raise an alarm per 3s: %d\n", sig);
    alarm(3);
}

int main(void)
{
    signal(SIGALRM, handle_alarm);
    alarm(3);
    while (1)
    {
        sleep(1);
    }
    return 0;
}

setitimer

  • 头文件<sys/time.h>
  • 根据设置的首次触发时间和时间间隔触发SIGALRM信号
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>

void handle(int sig)
{
    printf("raise an alarm: %d\n", sig);   //will print 14
}

int main()
{
    signal(SIGALRM, handle);

    //will raise an alarm signal after 3s and ther per 1s
    struct itimerspec value = {1,0,3,0};
    setitimer(CLOCK_REALTIME, &value, NULL);   

    while (1);
}

POSIX定时器

  • 头文件<time.h>,需要链接库rt
  • 函数:timer_create、timer_settime、timer_delete

定时触发信号

#include <stdio.h>
#include <time.h>
#include <signal.h>

void handle_alarm(int sig)
{
    printf("raise an alarm signal %d\n", sig);
}

int main()
{
    signal(SIGALRM, handle_alarm);		//设置信号处理函数
    
    timer_t timer;
    struct sigevent evp = {0};			//初始化全0
    evp.sigev_notify = SIGEV_SIGNAL;	//通知类型为通过信号
    evp.sigev_signo = SIGALRM;
    timer_create(CLOCK_REALTIME, &evp, &timer);

    struct itimerspec value = {1, 0,3,0};		//3s触发1次,然后每隔1s触发1次
    timer_settime(timer, 0, &value, NULL);		//flags为0表示value是相对时间
    
    while (1);
}

定时创建新线程执行自定义函数

#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <pthread.h>

void handle(sigval_t val)
{
    printf("timer raise to create a new thread[%lu]: %d\n",
        pthread_self(), val.sival_int);   //will print new thread_id and 66
}

int main()
{
    printf("main thread[%lu]\n", pthread_self());

    timer_t timer;						//定时器id
    struct sigevent evp = {0};			//初始化全0
    evp.sigev_notify = SIGEV_THREAD;
    evp.sigev_notify_function= handle;	//线程执行函数
    evp.sigev_value.sival_int = 66;     //处理函数的参数

    timer_create(CLOCK_REALTIME, &evp, &timer);

    struct itimerspec value = {1, 0,3,0};	//3s触发1次,然后每隔1s触发1次
    timer_settime(timer, 0, &value, NULL);	//flags为0表示value是相对时间

    while (1);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值