Skip to content

Instantly share code, notes, and snippets.

@b4284
Created May 20, 2021 17:07
Show Gist options
  • Save b4284/cb1bf564c78451fedaa51a336aff604b to your computer and use it in GitHub Desktop.
Save b4284/cb1bf564c78451fedaa51a336aff604b to your computer and use it in GitHub Desktop.
#include <signal.h>
#include <time.h>
#include <stdio.h>
#include <signal.h>
void sh(int sig)
{
for (double f = 0; f < 10; f += 0.5) {
printf(" f = %g\n", f);
usleep(100000);
}
}
void main()
{
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_handler = &sh;
sa.sa_flags = 0;
int c = sigaction(SIGALRM, &sa, NULL);
printf("c = %d\n", c);
timer_t tt;
int a = timer_create(CLOCK_REALTIME, NULL, &tt);
printf("a = %d\n", a);
struct itimerspec it;
it.it_interval.tv_sec = 0;
it.it_interval.tv_nsec = 0;
it.it_value.tv_sec = 1;
it.it_value.tv_nsec = 0;
int b = timer_settime(tt, 0, &it, NULL);
printf("b = %d\n", b);
for (int i = 0; i < 20; i++) {
printf(" i = %d\n", i);
usleep(100000);
}
}
@b4284
Copy link
Author

b4284 commented May 20, 2021

Note: Perl document says it's not okay to use sleep() and signals at the same time, because sleep() might be implemented with signals as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment