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; }
bool setscheduleTest() { struct sched_param_ex sp; sp.sched_priority = 5; sp.requested_time = 200; sp.num_cooloff = 3; ASSERT_TEST(sched_setscheduler(getpid(), SCHED_RR, (struct sched_param *)&sp) == 0); ASSERT_TEST(sched_setscheduler(getpid(), SCHED_SHORT, (struct sched_param *)&sp) == -1); ASSERT_TEST(errno == EPERM); sp.sched_priority = -5; sp.requested_time = -200; sp.num_cooloff = -3; ASSERT_TEST(sched_setscheduler(getpid(), SCHED_SHORT, (struct sched_param *)&sp) == -1); ASSERT_TEST(errno == EPERM); sp.sched_priority = 0; ASSERT_TEST(sched_setscheduler(getpid(), SCHED_OTHER, (struct sched_param *)&sp) == 0); sp.requested_time = 0; sp.num_cooloff = 3; ASSERT_TEST(sched_setscheduler(getpid(), SCHED_SHORT, (struct sched_param *)&sp) == -1); ASSERT_TEST(errno == EINVAL); sp.requested_time = 500; sp.num_cooloff = 6; ASSERT_TEST(sched_setscheduler(getpid(), SCHED_SHORT, (struct sched_param *)&sp) == -1); ASSERT_TEST(errno == EINVAL); sp.sched_priority = -5000; sp.requested_time = 501; sp.num_cooloff = 3; struct sched_param_ex sp1; ASSERT_TEST(sched_setscheduler(getpid(), SCHED_SHORT, (struct sched_param *)&sp) == 0); ASSERT_TEST(remaining_cooloffs(getpid()) == 3); ASSERT_TEST(sched_getparam(getpid(),(struct sched_param *)&sp1) == 0); ASSERT_TEST(sp1.requested_time == 501); ASSERT_TEST(is_SHORT(getpid())); ASSERT_TEST(sched_setscheduler(getpid(), SCHED_SHORT, (struct sched_param *)&sp) == -1); ASSERT_TEST(errno == EPERM); ASSERT_TEST(sched_setscheduler(getpid(), SCHED_OTHER, (struct sched_param *)&sp) == -1); ASSERT_TEST(errno == EPERM); return true; }
int test1() { int child = fork(); int i; data_t* smem; pid_t mypid; int cooloffs = 5; sched_param_t params = {1, 50, cooloffs}; int status; int counter = 0; if (child != 0) { smem = (data_t*)make_shared(sizeof(data_t), 1); smem->curr = 0; mypid = getpid(); ASSERT_POSITIVE(mypid); nice(1); // be nicer than child ASSERT_ZERO(sched_setscheduler(mypid, SCHED_SHORT, ¶ms)); ASSERT_EQUALS(sched_setscheduler(mypid, SCHED_SHORT, ¶ms), -1); ASSERT_EQUALS(errno, EPERM); ASSERT_EQUALS(is_SHORT(mypid), 1); smem->arr[smem->curr] = FATHER+0; // init value ASSERT_ZERO(sched_setscheduler(child, SCHED_SHORT, ¶ms)); // now we lost control until child will be overdue // child got into overdue. we gained control again. we should still be short here. smem->arr[++smem->curr] = FATHER+1; while (is_SHORT(mypid)) ; smem->arr[++smem->curr] = FATHER+(1*10)+OVERDUE_PERIOD; // got into first overdue period ASSERT_EQUALS(remaining_cooloffs(mypid), cooloffs-1); for (i = 1; i <= cooloffs; ++i) { while (!is_SHORT(mypid)) ; smem->arr[++smem->curr] = FATHER+(i*10); // got out of overdue period ASSERT_EQUALS(remaining_cooloffs(mypid), cooloffs-i); while (is_SHORT(mypid)) ; smem->arr[++smem->curr] = FATHER+((i+1)*10)+OVERDUE_PERIOD; // got into overdue period ASSERT_EQUALS(remaining_cooloffs(mypid), max(cooloffs-(i+1), 0)); } // now should be overdue forever ASSERT_ZERO(remaining_cooloffs(mypid)); waitpid(child, &status, 0); // use `gcc -DVERBOSE ...` in order to print the array state if (IS_VERBOSE()) { for (i = 0; i <= 24; i++) { printf("%d:\t%d\n", i, smem->arr[i]); } } // check array ASSERT_EQUALS(smem->arr[0], FATHER+0); ASSERT_EQUALS(smem->arr[1], SON+0); ASSERT_EQUALS(smem->arr[2], FATHER+1); ASSERT_EQUALS(smem->arr[3], SON+10+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[4], SON+10); ASSERT_EQUALS(smem->arr[5], FATHER+10+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[6], FATHER+10); ASSERT_EQUALS(smem->arr[7], SON+20+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[8], SON+20); ASSERT_EQUALS(smem->arr[9], FATHER+20+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[10], FATHER+20); ASSERT_EQUALS(smem->arr[11], SON+30+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[12], SON+30); ASSERT_EQUALS(smem->arr[13], FATHER+30+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[14], FATHER+30); ASSERT_EQUALS(smem->arr[15], SON+40+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[16], SON+40); ASSERT_EQUALS(smem->arr[17], FATHER+40+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[18], FATHER+40); ASSERT_EQUALS(smem->arr[19], SON+50+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[20], SON+50); ASSERT_EQUALS(smem->arr[21], FATHER+50+OVERDUE_PERIOD); ASSERT_EQUALS(smem->arr[22], FATHER+50); ASSERT_EQUALS(smem->arr[23], SON+60+OVERDUE_PERIOD); // son finished ASSERT_EQUALS(smem->arr[24], FATHER+60+OVERDUE_PERIOD); return 1; } else { pid_t mypid = getpid(); ASSERT_POSITIVE(mypid); while (is_SHORT(mypid) != 1) ; data_t* smem = (data_t*)make_shared(sizeof(data_t), 0); smem->arr[++smem->curr] = SON+0; // this is the first SHORT time slice while (is_SHORT(mypid)) ; smem->arr[++smem->curr] = SON+(1*10)+OVERDUE_PERIOD; // got into first overdue period ASSERT_EQUALS(remaining_cooloffs(mypid), cooloffs-1); for (i = 1; i <= cooloffs; ++i) { while (!is_SHORT(mypid)) ; smem->arr[++smem->curr] = SON+(i*10); // got out of overdue period ASSERT_EQUALS(remaining_cooloffs(mypid), cooloffs-i); while (is_SHORT(mypid)) ; smem->arr[++smem->curr] = SON+((i+1)*10)+OVERDUE_PERIOD; // got into overdue period ASSERT_EQUALS(remaining_cooloffs(mypid), max(cooloffs-(i+1), 0)); } // now should be overdue forever ASSERT_ZERO(remaining_cooloffs(mypid)); exit(0); } return 0; }