Last active
August 29, 2015 13:56
-
-
Save 5kg/9003691 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <math.h> | |
| #include <signal.h> | |
| #include <unistd.h> | |
| #include <sys/time.h> | |
| #define INTERVAL 50000 | |
| #define SAMPLES 200 | |
| int sleep_interval[SAMPLES]; | |
| void sleep_a_while(int sig) | |
| { | |
| static int idx = 0; | |
| usleep(sleep_interval[idx++]); | |
| idx %= SAMPLES; | |
| } | |
| void init_sleep_interval(void) | |
| { | |
| int i; | |
| for (i = 0; i < SAMPLES; ++i) | |
| sleep_interval[i] = (sin(M_PI*2*i/SAMPLES) + 1) * INTERVAL / 2; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| init_sleep_interval(); | |
| signal(SIGALRM, sleep_a_while); | |
| struct itimerval timer = { | |
| .it_interval = { .tv_sec = 0, .tv_usec = INTERVAL }, | |
| .it_value = {.tv_sec = 0, .tv_usec = INTERVAL }, | |
| }; | |
| setitimer(ITIMER_REAL, &timer, NULL); | |
| for (;;) | |
| ; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <math.h> | |
| #include <signal.h> | |
| #include <unistd.h> | |
| #include <sys/time.h> | |
| #define INTERVAL 50000 | |
| #define SAMPLES 200 | |
| int sleep_interval[SAMPLES]; | |
| void init_sleep_interval(void) | |
| { | |
| int i; | |
| for (i = 0; i < SAMPLES; ++i) | |
| sleep_interval[i] = (sin(M_PI*2*i/SAMPLES) + 1) * INTERVAL / 2; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| struct timeval hd, tl; | |
| int idx = 0; | |
| init_sleep_interval(); | |
| for (;;) { | |
| gettimeofday(&hd, NULL); | |
| tl = hd; | |
| while (tl.tv_sec * 1000000 + tl.tv_usec - hd.tv_sec * 1000000 - hd.tv_usec < INTERVAL - sleep_interval[idx]) | |
| gettimeofday(&tl, NULL); | |
| usleep(sleep_interval[idx++]); | |
| idx %= SAMPLES; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment