Advertisement
CSenshi

Untitled

Apr 26th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. /* This method is helper part of thread_tick which is called by interrupt handler.
  2. */
  3. void four_tick_priority(struct thread *cur, int my_ticks)
  4. {
  5. if (my_ticks % TIME_SLICE == 0)
  6. {
  7. // as said in hm we have to change priority in once every fourth clock tick.
  8. cur->priority = check_edge_priority(fix_int(fix_priority(PRI_MAX, cur->recent_cpu, cur->nice)));
  9. // cur_thread->priority = check_edge_priority(fix_int(fix_priority(PRI_MAX, cur_thread->recent_cpu, cur_thread->nice)));
  10. }
  11. }
  12.  
  13. /* This is helper method for task3.
  14. if priority is out of edge we put them in 0-63 range!
  15. */
  16. int check_edge_priority(fixed_point_t prior)
  17. {
  18. int result = fix_trunc(prior);
  19. if (result < 0)
  20. result = PRI_MIN;
  21.  
  22. if (result > PRI_MAX)
  23. result = PRI_MAX;
  24.  
  25. return result;
  26. }
  27.  
  28.  
  29.  
  30.  
  31. void thread_tick(void)
  32. {
  33. struct thread *t = thread_current();
  34. int my_ticks = timer_ticks();
  35. if (check_scheduler_alg())
  36. {
  37. // as said every time interrupt handler called increment recent_cpu
  38. t->recent_cpu = fix_increment_cpu(t->recent_cpu);
  39. //checking for priority!
  40. four_tick_priority(t, my_ticks);
  41. //checking for
  42. //if true we have to write task 3 logic!
  43. // i mean as asked in hm timer_ticks() % TIMER_FREQ == 0 only thats when we have to to calculations.
  44. if (my_ticks % TIMER_FREQ == 0)
  45. {
  46. load_avg = fix_load_avg(load_avg, ready_threads);
  47. // here will go checking for reaaaadyy queueuee
  48. struct list_elem *elems;
  49. for (elems = list_begin(&all_list); elems != list_end(&all_list);)
  50. {
  51. struct thread *cur_thread = list_entry(elems, struct thread, elem);
  52. cur_thread->recent_cpu = fix_recent_cpu(load_avg, t->recent_cpu, t->nice);
  53.  
  54. if (cur_thread->status == THREAD_READY || cur_thread->status == THREAD_RUNNING)
  55. {
  56.  
  57. struct list_elem *tmp = elems;
  58. elems = list_next(elems);
  59. list_remove(tmp);
  60. cur_thread->priority = check_edge_priority(fix_int(fix_priority(PRI_MAX, cur_thread->recent_cpu, cur_thread->nice)));
  61. // printf('%d\n', cur_thread->priority);
  62. // list_push_front(&all_list, &t->elem);
  63. // if (cur_thread->status == THREAD_READY)
  64. // thread_unblock(cur_thread);
  65. list_arr_insert_pr(&cur_thread->elem, PRI_MAX, t->priority);
  66. }
  67. else
  68. {
  69. elems = list_next(elems);
  70. }
  71. }
  72. }
  73. }
  74. /* Update statistics. */
  75. if (t == idle_thread)
  76. idle_ticks++;
  77. #ifdef USERPROG
  78. else if (t->pagedir != NULL)
  79. user_ticks++;
  80. #endif
  81. else
  82. kernel_ticks++;
  83.  
  84. /* Enforce preemption. */
  85. if (++thread_ticks >= TIME_SLICE)
  86. intr_yield_on_return();
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement