diff --git a/20160523/alarm-2.c b/20160523/alarm-2.c new file mode 100644 index 0000000000000000000000000000000000000000..a36cb541fd2183397262c1555e0476a2a1325782 --- /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 0000000000000000000000000000000000000000..18783588e23f360a9d774d2bdb3713cc0a68fa08 --- /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); +}