#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sched.h>
int main(int argc, char **argv) {
    double duty = atof(argv[1]);   /* busy ms per period */
    double period = atof(argv[2]); /* period ms */
    for (;;) {
        struct timespec start, now;
        clock_gettime(CLOCK_MONOTONIC, &start);
        for (;;) {
            clock_gettime(CLOCK_MONOTONIC, &now);
            double el = (now.tv_sec - start.tv_sec) * 1000.0 + (now.tv_nsec - start.tv_nsec) / 1e6;
            if (el >= duty) break;
        }
        struct timespec ts;
        ts.tv_sec = 0;
        ts.tv_nsec = (long)((period - duty) * 1e6);
        nanosleep(&ts, NULL);
    }
}
