timer.c

来自「interrupt application code * compile :」· C语言 代码 · 共 62 行

C
62
字号
#include <stdio.h>#include <sys/time.h>#include <signal.h>struct timeval tpstart,tpend;float  timeuse;static timer_count = 0;void prompt_info(int signo){    time_t t = time(NULL);    /* [1] 2 seconds turned, print something */    printf("[%d] prompt_info called\n", ++timer_count);    /* [2] get current time and print it */    ctime(&t);    printf("    current time %s", ctime(&t));      /* [3] stop get time, and print it */    gettimeofday(&tpend,NULL);    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;    timeuse/=1000000;    printf("    Used Time:%f\n",timeuse); } void init_sigaction(void){    struct sigaction act;    act.sa_handler=prompt_info;    act.sa_flags=0;    sigemptyset(&act.sa_mask);    sigaction(SIGPROF,&act,NULL);    /* begin get the time */    gettimeofday(&tpstart,NULL);}void init_time(){    struct itimerval value;    value.it_value.tv_sec=2;    value.it_value.tv_usec=0;    value.it_interval=value.it_value;    setitimer(ITIMER_PROF,&value,NULL);}/* * timer application code * compile : *          $/usr/local/arm/2.95.3/bin/arm-linux-gcc -o timer timer.c *          $cp timer /tftpboot/examples * run in target: *          #mount 192.168.1.180:/tftpboot/ /mnt/nfs *          #cd /mnt/nfs/examples *          #./timer */int main(int argc, char **argv){    init_sigaction();    init_time();    while(1);    exit(0);} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?