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; }
void line_of_centers_analyze(const line_of_centers_t& loc, real omega, std::pair<real, real>& rho1_max, std::pair<real, real>& rho2_max, std::pair<real, real>& l1_phi, std::pair<real, real>& l2_phi, std::pair<real, real>& l3_phi, real& rho1_phi, real& rho2_phi) { for (auto& l : loc) { ASSERT_NONAN(l.first); for (integer f = 0; f != NF + NGF; ++f) { ASSERT_NONAN(l.second[f]); } } rho1_max.second = rho2_max.second = 0.0; integer rho1_maxi, rho2_maxi; /// printf( "LOCSIZE %i\n", loc.size()); for (integer i = 0; i != loc.size(); ++i) { const real x = loc[i].first; const real rho = loc[i].second[rho_i]; const real pot = loc[i].second[pot_i]; // printf( "%e %e\n", x, rho); if (rho1_max.second < rho) { // printf( "!\n"); rho1_max.second = rho; rho1_max.first = x; rho1_maxi = i; real phi_eff = pot / ASSERT_POSITIVE(rho) - 0.5 * x * x * omega * omega; rho1_phi = phi_eff; } } for (integer i = 0; i != loc.size(); ++i) { const real x = loc[i].first; if (x * rho1_max.first < 0.0) { const real rho = loc[i].second[rho_i]; const real pot = loc[i].second[pot_i]; if (rho2_max.second < rho) { rho2_max.second = rho; rho2_max.first = x; rho2_maxi = i; real phi_eff = pot / ASSERT_POSITIVE(rho) - 0.5 * x * x * omega * omega; rho2_phi = phi_eff; } } } l1_phi.second = -std::numeric_limits < real > ::max(); l2_phi.second = -std::numeric_limits < real > ::max(); l3_phi.second = -std::numeric_limits < real > ::max(); for (integer i = 0; i != loc.size(); ++i) { const real x = loc[i].first; const real rho = loc[i].second[rho_i]; const real pot = loc[i].second[pot_i]; real phi_eff = pot / ASSERT_POSITIVE(rho) - 0.5 * x * x * omega * omega; if (x > std::min(rho1_max.first, rho2_max.first) && x < std::max(rho1_max.first, rho2_max.first)) { if (phi_eff > l1_phi.second) { l1_phi.second = phi_eff; l1_phi.first = x; } } else if (std::abs(x) > std::abs(rho2_max.first) && x * rho2_max.first > 0.0) { if (phi_eff > l2_phi.second) { l2_phi.second = phi_eff; l2_phi.first = x; } } else if (std::abs(x) > std::abs(rho1_max.first)) { if (phi_eff > l3_phi.second) { l3_phi.second = phi_eff; l3_phi.first = x; } } } }