From 49dfd467e87c4ec5b009d381d5290b8de6c3f1fc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Christian=20L=C3=B6pke?= <loepke@edfritsch.de>
Date: Mon, 23 May 2016 15:04:20 +0200
Subject: [PATCH] =?UTF-8?q?Interruptbeispiel=20hinzugef=C3=BCgt=20(IRealTi?=
 =?UTF-8?q?mer=20mittels=20manueller=20alarm()=20Nachstellung=20alle=201?=
 =?UTF-8?q?=20Sekunde.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 20160523/alarm-2.c | 37 +++++++++++++++++++++++++++++++++++++
 20160523/alarm.c   | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 20160523/alarm-2.c
 create mode 100644 20160523/alarm.c

diff --git a/20160523/alarm-2.c b/20160523/alarm-2.c
new file mode 100644
index 0000000..a36cb54
--- /dev/null
+++ b/20160523/alarm-2.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/time.h>
+
+volatile sig_atomic_t keep_going = 1;
+struct timeval tv;
+int uOffset;
+
+void exitHandler(int sig)
+{
+  keep_going = 0;
+}
+
+void catchAlarm (int sig)
+{
+  if(keep_going)
+    alarm(10);
+  gettimeofday(&tv, NULL); //Get current time
+
+  printf("Hello World at %d seconds and %d microseconds! us between: %d microseconds!\n", 
+(int)tv.tv_sec, (int)tv.tv_usec, (int)tv.tv_usec - uOffset);
+  uOffset = (int)tv.tv_usec;
+}
+
+
+int main(void)
+{
+  keep_going = 1;
+  uOffset = 0;
+
+  signal(SIGINT, exitHandler);
+  signal(SIGALRM, catchAlarm);
+  alarm(10);
+  while(keep_going);
+}
diff --git a/20160523/alarm.c b/20160523/alarm.c
new file mode 100644
index 0000000..1878358
--- /dev/null
+++ b/20160523/alarm.c
@@ -0,0 +1,43 @@
+/*
+  ALARM alle 1 Sekunden mittels IRealTimers und manueller Aktivierung des Timers mittels alarm().
+  Nachteil gegenüber einmaligem Intervall Setup: einige µs gehen bei jedem "nachstellen" des Int.
+  in catchAlarm() verloren.
+ */
+
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/time.h>
+
+volatile sig_atomic_t keep_going = 1;
+struct timeval tv;
+int uOffset;
+
+void exitHandler(int sig)
+{
+  keep_going = 0;
+}
+
+void catchAlarm (int sig)
+{
+  if(keep_going)
+    alarm(1);
+  gettimeofday(&tv, NULL); //Get current time
+
+  printf("Hello World at %d seconds and %d microseconds! us between: %d microseconds!\n", 
+(int)tv.tv_sec, (int)tv.tv_usec, (int)tv.tv_usec - uOffset);
+  uOffset = (int)tv.tv_usec;
+}
+
+
+int main(void)
+{
+  keep_going = 1;
+  uOffset = 0;
+
+  signal(SIGINT, exitHandler);
+  signal(SIGALRM, catchAlarm);
+  alarm(1);
+  while(keep_going);
+}
-- 
GitLab