Skip to content
Snippets Groups Projects
Commit 49dfd467 authored by Christian Löpke's avatar Christian Löpke
Browse files

Interruptbeispiel hinzugefügt (IRealTimer mittels manueller alarm() Nachstellung alle 1 Sekunde.

parent 6f7aa202
No related branches found
No related tags found
No related merge requests found
Pipeline #
#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);
}
/*
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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment