linux c 程序计时器,Linux下C语言中的简单定时器样例

本文最后更新于2017年8月5日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

=Start=

缘由:

学习需要

正文:

参考解答:

alarm()+signal()/sigaction()实现的不那么精确的定时器功能

#include

#include

#include

#include /* struct timeval */

#include /* time() */

#include

#include

#include

void print_current_time_with_ms(void)

{

long ms; // Milliseconds

time_t s; // Seconds

struct timespec spec;

clock_gettime(CLOCK_REALTIME, &spec);

s = spec.tv_sec;

ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds

printf("Current time: %"PRIdMAX".%03ld seconds since the Epoch\n", (intmax_t)s, ms);

}

void sigalrm_fn(int sig)

{

print_current_time_with_ms();

alarm(2);// every 2 seconds

return;

}

int main(void)

{

signal(SIGALRM, sigalrm_fn);

alarm(1);// first call ?

while(1)

pause();

return 0;

}

/* $ ./a.out

Current time: 1501747559.026 seconds since the Epoch

Current time: 1501747561.026 seconds since the Epoch

Current time: 1501747563.028 seconds since the Epoch

Current time: 1501747565.032 seconds since the Epoch

Current time: 1501747567.035 seconds since the Epoch

*/

setitimer()+sigaction()的一个实现

#include

#include

#include

int limit = 10;

/* signal process */

void timeout_info(int signo)

{

if(limit == 0)

{

printf("Sorry, time limit reached.\n");

return;

}

printf("Only %d seconds left.\n", limit--);

}

/* init sigaction */

void init_sigaction(void)

{

struct sigaction act;

act.sa_handler = timeout_info;

act.sa_flags = 0;

sigemptyset(&act.sa_mask);

sigaction(SIGPROF, &act, NULL);

}

/* init */

void init_time(void)

{

struct itimerval val;

val.it_value.tv_sec = 1;

val.it_value.tv_usec = 0;

val.it_interval = val.it_value;

setitimer(ITIMER_PROF, &val, NULL);

}

int main(void)

{

init_sigaction();

init_time();

printf("You have only 10 seconds for thinking.\n");

while(1);

return 0;

}

多个setitimer()的示例

#include

#include

#include

#include

#include

#include

#include

#include

void print_current_time_with_ms(void)

{

long ms; // Milliseconds

time_t s; // Seconds

struct timespec spec;

clock_gettime(CLOCK_REALTIME, &spec);

s = spec.tv_sec;

ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds

printf("Current time: %"PRIdMAX".%03ld seconds since the Epoch\n", (intmax_t)s, ms);

}

int sec = 5;

void sigroutine(int signo){

switch (signo) {

case SIGALRM:

printf("Catch a signal -- SIGALRM\n");

print_current_time_with_ms();

signal(SIGALRM, sigroutine);

break;

case SIGVTALRM:

printf("Catch a signal -- SIGVTALRM\n");

print_current_time_with_ms();

signal(SIGVTALRM, sigroutine);

break;

}

return;

}

int main() {

printf("process id is %d\n", getpid());

signal(SIGALRM, sigroutine);

signal(SIGVTALRM, sigroutine);

struct itimerval value, ovalue, value2; //(1)

value.it_value.tv_sec = 1;

value.it_value.tv_usec = 0;

value.it_interval.tv_sec = 1;

value.it_interval.tv_usec = 0;

setitimer(ITIMER_REAL, &value, &ovalue); //(2)

value2.it_value.tv_sec = 0;

value2.it_value.tv_usec = 500000;

value2.it_interval.tv_sec = 0;

value2.it_interval.tv_usec = 500000;

setitimer(ITIMER_VIRTUAL, &value2, &ovalue);

for(;;)

;

return 0;

}

参考链接:

=END=

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值