Sowmya Msk
Sowmya Msk

Reputation: 51

How to implement a timer in c?

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

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 while loop for sending the packet is being executed. For example:

    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.

Upvotes: 5

Views: 39618

Answers (7)

Lance Richardson
Lance Richardson

Reputation: 4610

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.

Upvotes: 8

Abhishek Sagar
Abhishek Sagar

Reputation: 1336

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

Upvotes: 0

HuntM
HuntM

Reputation: 157

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

    #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.

Upvotes: 2

Fred
Fred

Reputation: 17095

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

Upvotes: 3

JeremyP
JeremyP

Reputation: 86691

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

Edit changed 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);

Upvotes: 6

unwind
unwind

Reputation: 400129

Look at the standard time() function.

Upvotes: 2

elcuco
elcuco

Reputation: 9208

man 3 sleep:

NAME sleep - Sleep for the specified number of seconds

SYNOPSIS #include < unistd.h >

   unsigned int sleep(unsigned int seconds);

Upvotes: -2

Related Questions