需要帮助在Linux中用C语言应用计时器

时间:2011-04-22 12:02:18

标签: c linux

我想在我们的C程序中创建一个计时器,以便它可以在每1秒后打印一次变量。

有人可以帮我这么做吗?

6 个答案:

答案 0 :(得分:4)

不要使用忙等待,因为你有100%的CPU利用率。 您必须使用系统功能将进程转换为休眠模式,例如select()

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

void your_callback()
{
    printf("%s\n", __FUNCTION__);
}

int main()
{
    struct timeval t;

    while (1) {
        t.tv_sec = 1;
        t.tv_usec = 0;

        select(0, NULL, NULL, NULL, &t);

        your_callback();
    }

    return 0;
}

答案 1 :(得分:3)

如果 所有 ,您有兴趣做的是以一秒为间隔打印变量的值,使用time(2)clock(3)作为在其他答案中建议可能就足够了。一般来说,我不推荐这些busy-waiting技术。


如果您的程序更复杂,我建议您使用 alarm(2) settimer(2)函数进行调查,以一秒的间隔异步向您的应用程序发送信号。< / p>

以下示例使用select(2)无限期阻塞,以最大限度地减少与忙等待技术相关的CPU使用率。阻塞select()调用被中断,并在捕获信号时返回。对于SIGALRM信号,设置print_variable标志并打印variable的值。

示例1: 使用alarm()

#include <signal.h>
#include <stdio.h>
#include <sys/select.h>
#include <unistd.h>

volatile unsigned int variable = 0;
volatile unsigned int print_variable = 0;

void alarm_handler(int signum)
{
    variable++;
    print_variable = 1;
    alarm(1);
}

int main()
{        
    signal(SIGALRM, alarm_handler);
    alarm(1);

    for (;;)
    {
        select(0, NULL, NULL, NULL, NULL);

        if (print_variable)
        {
            printf("Variable = %u\n", variable);
        }
    }
}

注意:为简单起见,上述代码中省略了错误检查。

可能在printf()处理程序中调用SIGALRM函数,但通常不鼓励在信号处理程序中调用非重入函数。


也可以将一秒的超时传递给select(),但如果它被任何信号中断,则需要额外的逻辑来确保一秒钟超时的剩余时间得到遵守。幸运的是,在Linux上,select()修改超时值以反映未睡眠的时间。这允许检测中断情况,然后是后续调用select()以完成超时。

示例2: 使用select()

#include <errno.h>
#include <stdio.h>
#include <sys/select.h>

volatile unsigned int variable = 0;

int main()
{
    struct timeval tv;
    int val;

    for (;;)
    {
        tv.tv_sec = 1;
        tv.tv_usec = 0;

        do
        {
            val = select(0, NULL, NULL, NULL, &tv);
        } while (val != 0 && errno == EINTR);

        printf("Variable = %u\n", ++variable);
    }
}

答案 2 :(得分:1)

如果你只想要第二精度。如果包含time(0),请使用time.h返回当前时间。

<强>更新 添加在10秒内每秒打印20的简单示例:

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

int main()
{
    int a = 10;
    int num = 20;
    int c = time(0);
    while(n--)
    {
        printf("%d\n", a);
        while(!(time(0) - c));
        c = time(0);
    }
    return 0;
}

答案 3 :(得分:0)

使用time(0)查看此示例

/* timer.c */ 
#include <stdio.h> 
#include <time.h> 

void delay_sec( int seconds ){ 
    clock_t endwait; 
    endwait = clock () + seconds * CLOCKS_PER_SEC; 
    while (clock() < endwait) {} 
} 

int main (void){ 
    time_t rawtime, ini_time, now; 
    struct tm *ptm; 

    time ( &ini_time ); 
    for(;;){ 
        time ( &rawtime ); 
        //ptm = gmtime ( &rawtime ); 
        //printf ("%2d:%02d:%02d\n", ptm_2->tm_hour, ptm_2->tm_min, ptm_2->tm_sec); 
        now = rawtime - ini_time; 
        ptm = gmtime ( &now ); 
        printf ("%2d:%02d:%02d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); 
        delay_sec(1); 
    } 
    return 0; 
}  

答案 4 :(得分:0)

我相信您知道1000 Milliseconds等于1 Second

#include <stdio.h>  
#include <time.h>
#define mydelay 1000

void delay(int mseconds)
{
    clock_t wait = mseconds + clock();
    while (wait > clock());
}

int main()  
{  
   int i=100;
   while(1)
   {
           printf("%d\n",i);
           delay(mydelay);
   }              
   return 0;  
}

答案 5 :(得分:0)

一个简单的例子,每隔1秒打印一次变量a的值:

#include<stdio.h>

void main(void)
{
  int a = 10;
  while(a--)
  {
    printf("Value of a = %d\n", a);
    sleep(1);  
  }

}

输出:

  

a = 9的值   ...
  值a = 0

相关问题