《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

12 years ago
  1. #include <defs.h>
  2. #include <list.h>
  3. #include <proc.h>
  4. #include <assert.h>
  5. #include <default_sched.h>
  6. static void
  7. RR_init(struct run_queue *rq) {
  8. list_init(&(rq->run_list));
  9. rq->proc_num = 0;
  10. }
  11. static void
  12. RR_enqueue(struct run_queue *rq, struct proc_struct *proc) {
  13. assert(list_empty(&(proc->run_link)));
  14. list_add_before(&(rq->run_list), &(proc->run_link));
  15. if (proc->time_slice == 0 || proc->time_slice > rq->max_time_slice) {
  16. proc->time_slice = rq->max_time_slice;
  17. }
  18. proc->rq = rq;
  19. rq->proc_num ++;
  20. }
  21. static void
  22. RR_dequeue(struct run_queue *rq, struct proc_struct *proc) {
  23. assert(!list_empty(&(proc->run_link)) && proc->rq == rq);
  24. list_del_init(&(proc->run_link));
  25. rq->proc_num --;
  26. }
  27. static struct proc_struct *
  28. RR_pick_next(struct run_queue *rq) {
  29. list_entry_t *le = list_next(&(rq->run_list));
  30. if (le != &(rq->run_list)) {
  31. return le2proc(le, run_link);
  32. }
  33. return NULL;
  34. }
  35. static void
  36. RR_proc_tick(struct run_queue *rq, struct proc_struct *proc) {
  37. if (proc->time_slice > 0) {
  38. proc->time_slice --;
  39. }
  40. if (proc->time_slice == 0) {
  41. proc->need_resched = 1;
  42. }
  43. }
  44. struct sched_class default_sched_class = {
  45. .name = "RR_scheduler",
  46. .init = RR_init,
  47. .enqueue = RR_enqueue,
  48. .dequeue = RR_dequeue,
  49. .pick_next = RR_pick_next,
  50. .proc_tick = RR_proc_tick,
  51. };