remaining_time remaining_milliseconds() const { if(is_sentinel()) { return remaining_time(win32::infinite); } else if(relative) { unsigned long const now=win32::GetTickCount(); unsigned long const elapsed=now-start; return remaining_time((elapsed<milliseconds)?(milliseconds-elapsed):0); } else { system_time const now=get_system_time(); if(abs_time<=now) { return remaining_time(0); } return remaining_time((abs_time-now).total_milliseconds()+1); } }
static void* input_handler_thread(thread_handler* h) { struct timespec start, end; input_handler* ih = (input_handler*) h; clock_gettime(CLOCK_REALTIME, &start); while(!h->cancel) { input_handler_poll(ih); clock_gettime(CLOCK_REALTIME, &end); struct timespec t = remaining_time(&ih->period, &start, &end); nanosleep(&t, NULL); start = end; } return NULL; }
bool shortFork() { pid_t pid; struct sched_param_ex sp; int status; sp.sched_priority = 0; sp.requested_time = 3000; sp.num_cooloff = 3; sched_setscheduler(getpid(), SCHED_SHORT, (struct sched_param *)&sp); //father becomes a SHORT pid = fork(); if(pid != 0) { ASSERT_TEST(remaining_time(getpid()) <= 1500); //if you didnt change sched_exit you could fail here! ASSERT_TEST(remaining_cooloffs(getpid()) == 1); } else { ASSERT_TEST(is_SHORT(getpid()) == 1); ASSERT_TEST(remaining_time(getpid()) <= 1500); ASSERT_TEST(remaining_cooloffs(getpid()) == 2); exit(0); } waitpid(pid, &status, 0); return true; }
int main(int argc, char const *argv[]) { int pid = getpid(); int res = 0; if((argc - 1) % 2 != 0) return -1; int tasks_num = (argc - 1) / 2; printf("task num: %d", tasks_num); test_task tasks[tasks_num]; int current = 0; struct sched_param sp; int i; for( i= 1; i <= tasks_num; i++) { test_task tt; tt.trials = atoi (argv[i*2 - 1]); tt.fnum = atoi (argv[i*2]); tt.pid = fork(); if(tt.pid == 0) { int cpid = getpid(); sp.sched_priority = 1; sp.requested_time = 2; sp.number_of_trials = tt.trials; sched_setscheduler(tt.pid, SCHED_SHORT, &sp); printf("test pid:\n"); printf("================\n"); res = is_SHORT(cpid); printf("is_SHORT res = %d;\terrno = %d;\tpid = %d\n", res, errno, cpid); res = remaining_time(cpid); printf("remaining_time res = %d;\terrno = %d;\tpid = %d\n", res, errno, cpid); res = remaining_trials(cpid); printf("remaining_trials res = %d;\terrno = %d;\tpid = %d\n", res, errno, cpid); if (!fork()){ return 0; } fibonaci(tt.fnum); return 0; }else if(tt.pid > 0) { tasks[current].pid = tt.pid; tasks[current].trials = tt.trials; tasks[current++].fnum = tt.fnum; } } int status = 0; res = 0; struct switch_info si[150] = {0}; for(i = 0; i < tasks_num; i++) { printf("waiting for %d\n", tasks[i].pid); res = waitpid(tasks[i].pid, &status, 0); } int size = get_scheduling_statistic(si); print_test_results(si, size, tasks, tasks_num); printf(" res = %d;errno = %d\n", res, errno); return 0; }