void exec_task(task_t *task, int loop_1ms, int simtime) { int k; /* do not use i! */ int nr_jobs = simtime / task->T; struct timeval tv, tv_C, tv_T, tv_D, tv_timeout; int ret = RET_SUCCESS; msecs_to_timeval(task->C, tv_C); msecs_to_timeval(task->T, tv_T); msecs_to_timeval(task->D, tv_D); msecs_to_timeval(DEFAULT_TIMEOUT, tv_timeout); /* get own PID. */ task->pid = getpid(); /* initialization for using RESCH. */ if (!rt_init()) { printf("Error: cannot begin!\n"); ret = RET_MISS; goto out; } xcpu_flag = 0; rt_set_priority(task->prio); rt_set_wcet(&tv_C); rt_set_period(&tv_T); rt_set_deadline(&tv_D); rt_reserve_cpu(&tv_C, xcpu_handler); rt_run(&tv_timeout); /* busy loop. */ for (k = 0; k < nr_jobs; k++) { while (!xcpu_flag) { gettimeofday(&tv, NULL); } if (!rt_wait_for_period()) { ret = RET_MISS; break; } xcpu_flag = 0; } rt_exit(); out: /* call _exit() but not exit(), since exit() may remove resources that are shared with the parent and other child processes. */ _exit(ret); /* no return. */ }
int main(int argc, char* argv[]) { int i; unsigned long prio; struct timespec period, runtime, timeout; struct timeval tv1, tv2, tv3; if (argc != 3) { printf("Error: invalid option\n"); } prio = atoi(argv[1]); /* priority. */ period = ms_to_timespec(atoi(argv[2])); /* period. */ runtime = ms_to_timespec(1000); /* execution time. */ timeout = ms_to_timespec(1000); /* timeout. */ /* bannar. */ printf("sample program\n"); rt_init(); rt_set_period(period); rt_set_runtime(runtime); rt_set_scheduler(SCHED_FP); /* you can also set SCHED_EDF. */ rt_set_priority(prio); rt_reserve_start(runtime, NULL); /* QoS is guaranteed. */ rt_run(timeout); for (i = 0; i < 20; i++) { gettimeofday(&tv1, NULL); printf("start %lu:%06lu\n", tv1.tv_sec, tv1.tv_usec); fflush(stdout); do { gettimeofday(&tv2, NULL); /* tv2 - tv1 = tv3 */ tvsub(&tv2, &tv1, &tv3); } while (tv3.tv_sec < 2); printf("finish %lu:%06lu\n", tv2.tv_sec, tv2.tv_usec); fflush(stdout); if (!rt_wait_period()) { printf("deadline is missed!\n"); } } rt_reserve_stop(); rt_exit(); return 0; }