Linux 如何在c中实现计时器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10189845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:50:11  来源:igfitidea点击:

How to implement a timer in c?

clinux

提问by Sowmya Msk

We want to add a timer to our C program under Linux platform.

我们想在Linux平台下为我们的C程序添加一个定时器。

We are trying to send the packets and we want to find out how many packets get sent in 1 minute. We want the timer to run at the same time as the whileloop for sending the packet is being executed. For example:

我们正在尝试发送数据包,我们想知道 1 分钟内发送了多少数据包。我们希望计时器在执行while发送数据包的循环的同时运行。例如:

    while(1)    
    {     
      send packets;    
    }

This loop will keep on sending the packets until ctrl-z is pressed. The timer should be used to stop the loop after 60 seconds.

这个循环将继续发送数据包,直到按下 ctrl-z。应使用计时器在 60 秒后停止循环。

回答by elcuco

man 3 sleep:

男人3睡觉:

NAME sleep - Sleep for the specified number of seconds

SYNOPSIS #include < unistd.h >

   unsigned int sleep(unsigned int seconds);

NAME sleep - 休眠指定秒数

概要 #include < unistd.h >

   unsigned int sleep(unsigned int seconds);

回答by unwind

Look at the standard time()function.

看标准time()函数。

回答by Fred

Can also check this http://www.gnu.org/software/libc/manual/html_node/Setting-an-Alarm.htmlto set timers which will send signals to your process and you can stop the while loop.

也可以检查这个http://www.gnu.org/software/libc/manual/html_node/Setting-an-Alarm.html来设置将向您的进程发送信号的计时器,您可以停止 while 循环。

回答by JeremyP

Just check the time on every iteration of the loop and when 1 minute has elapsed, count how many packets you have sent.

只需检查循环每次迭代的时间,当 1 分钟过去后,计算您发送的数据包数量。

Editchanged to reflect what the question actually asks!

编辑已更改以反映问题实际提出的问题!

time_t startTime = time(); // return current time in seconds
int numPackets = 0;
while (time() - startTime < 60)
{
    send packet
    numPackets++;
}
printf("Sent %d packets\n", numPackets);

回答by Lance Richardson

You could do something like this:

你可以这样做:

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

volatile int stop=0;

void sigalrm_handler( int sig )
{
    stop = 1;
}

int main(int argc, char **argv)
{
    struct sigaction sact;
    int num_sent = 0;
    sigemptyset(&sact.sa_mask);
    sact.sa_flags = 0;
    sact.sa_handler = sigalrm_handler;
    sigaction(SIGALRM, &sact, NULL);

    alarm(60);  /* Request SIGALRM in 60 seconds */
    while (!stop) {
        send_a_packet();
        num_sent++;
    }

    printf("sent %d packets\n", num_sent);
    exit(0);
}

If loop overhead turns out to be excessive, you could amortize the overhead by sending N packets per iteration and incrementing the count by N each iteration.

如果循环开销过大,您可以通过每次迭代发送 N 个数据包并在每次迭代中将计数增加 N 来分摊开销。

回答by HuntM

Here is code snippet of itimer that can be used for different time intervals on C with linux platform:

以下是可以在 Linux 平台上的 C 上用于不同时间间隔的计时器的代码片段:

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

    void timer_handler (int signum)
    {
     static int count = 0;
     printf ("timer expired %d times\n", ++count);
    }

    int main ()
    {
        struct sigaction sa;
        struct itimerval timer;

        /* Install timer_handler as the signal handler for SIGVTALRM. */
        memset (&sa, 0, sizeof (sa));
        sa.sa_handler = &timer_handler;
        sigaction (SIGVTALRM, &sa, NULL);

        /* Configure the timer to expire after 1 sec... */
        timer.it_value.tv_sec = 1;
        timer.it_value.tv_usec = 0;
        /* ... and every 1000 msec after that. */
        timer.it_interval.tv_sec = 1;
        timer.it_interval.tv_usec = 0;
        /* Start a virtual timer. It counts down whenever this process is
        *    executing. */
        setitimer (ITIMER_VIRTUAL, &timer, NULL);

        /* Do busy work. */
        while (1);
            sleep(1);
    }

hope it will help.

希望它会有所帮助。

回答by Abhishek Sagar

Use wheetimer (and its variant) data structures to implement timers.

使用 wheetimer(及其变体)数据结构来实现计时器。