Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

C timers

  • Loading branch information...
commit a928bc16343a8092bc6be24f41bdfda6f4fa75d0 1 parent 53407ec
@menudoproblema authored
View
40 c/timers/timer1.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/time.h>
+
+void do_something()
+{
+ printf("Do somenthing\n");
+ sleep(3);
+}
+
+int main()
+{
+ struct timeval tim;
+ int keepalive = 5, elapsed_seconds;
+ double t1, t2;
+
+
+ gettimeofday(&tim, NULL);
+ t1 = tim.tv_sec + (tim.tv_usec/1000000.0);
+
+ while(1)
+ {
+ gettimeofday(&tim, NULL);
+ t2 = tim.tv_sec + (tim.tv_usec/1000000.0);
+ elapsed_seconds = (int)(t2 - t1);
+ printf("t1: %f, t2: %f, elapsed_time:%d\n", t1, t2, elapsed_seconds);
+ if(elapsed_seconds > keepalive)
+ {
+ printf("Timeout!\n");
+ // Keep alive
+ gettimeofday(&tim, NULL);
+ t1 = tim.tv_sec + (tim.tv_usec/1000000.0);
+ }
+
+ do_something();
+
+ }
+
+ return 0;
+}
View
26 c/timers/timer1.c.md
@@ -0,0 +1,26 @@
+# Description
+
+This example prints ``Timeout!`` elapsed 5 seconds
+
+## When to use
+
+This timer is implemented in the program itself, so it is not strict. It
+ensures that the maintenance task to run every 5 seconds.
+
+Affected by blocking system calls and the application code itself. This
+code can be used when you need to perform a task after 5 seconds or more.
+
+# Compile
+
+ $ gcc -o timer1 timer1.c -Wall
+
+# Example
+
+ $ ./timer1
+ t1: 1333525126.992194, t2: 1333525126.992194, elapsed_time:0
+ Do somenthing
+ t1: 1333525126.992194, t2: 1333525129.992326, elapsed_time:3
+ Do somenthing
+ t1: 1333525126.992194, t2: 1333525132.992430, elapsed_time:6
+ Timeout!
+ Do somenthing
View
41 c/timers/timer2.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <signal.h>
+
+struct timeval tim;
+int keepalive = 5, elapsed_seconds;
+double t1, t2;
+
+
+void alive(int sig)
+{
+ gettimeofday(&tim, NULL);
+ t2 = tim.tv_sec + (tim.tv_usec/1000000.0);
+ elapsed_seconds = (int)(t2 - t1);
+ printf("Timeout! Elapsed time: %d\n", elapsed_seconds);
+
+ gettimeofday(&tim, NULL);
+ t1 = tim.tv_sec + (tim.tv_usec/1000000.0);
+
+ alarm(keepalive);
+}
+
+void do_something()
+{
+ printf("Do somenthing\n");
+ sleep(3);
+}
+
+int main() {
+ signal(SIGALRM, alive);
+ gettimeofday(&tim, NULL);
+ t1 = tim.tv_sec + (tim.tv_usec/1000000.0);
+
+ alarm(keepalive);
+ while(1)
+ {
+ do_something();
+ }
+ return 0;
+}
View
29 c/timers/timer2.c.md
@@ -0,0 +1,29 @@
+# Description
+
+This example prints ``Timeout! Elpased time: #s`` every 5 seconds
+
+## When to use
+
+This example uses a kernel timer through the alarm system call.
+Therefore it's necessary to handle signals. Use blocking system calls
+doesn't affect the operation of the timer.
+
+It can be used where necessary to perform a task accurately. For
+example, to keep alive a connection.
+
+# Compile
+
+ $ gcc -o timer2 timer2.c -Wall
+
+# Example
+
+ $ ./timer2
+ Do somenthing
+ Do somenthing
+ Timeout! Elapsed time: 5
+ Do somenthing
+ Do somenthing
+ Timeout! Elapsed time: 5
+ Do somenthing
+ Do somenthing
+ Timeout! Elapsed time: 5
Please sign in to comment.
Something went wrong with that request. Please try again.