Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This method is helper part of thread_tick which is called by interrupt handler.
- */
- void four_tick_priority(struct thread *cur, int my_ticks)
- {
- if (my_ticks % TIME_SLICE == 0)
- {
- // as said in hm we have to change priority in once every fourth clock tick.
- cur->priority = check_edge_priority(fix_int(fix_priority(PRI_MAX, cur->recent_cpu, cur->nice)));
- // cur_thread->priority = check_edge_priority(fix_int(fix_priority(PRI_MAX, cur_thread->recent_cpu, cur_thread->nice)));
- }
- }
- /* This is helper method for task3.
- if priority is out of edge we put them in 0-63 range!
- */
- int check_edge_priority(fixed_point_t prior)
- {
- int result = fix_trunc(prior);
- if (result < 0)
- result = PRI_MIN;
- if (result > PRI_MAX)
- result = PRI_MAX;
- return result;
- }
- void thread_tick(void)
- {
- struct thread *t = thread_current();
- int my_ticks = timer_ticks();
- if (check_scheduler_alg())
- {
- // as said every time interrupt handler called increment recent_cpu
- t->recent_cpu = fix_increment_cpu(t->recent_cpu);
- //checking for priority!
- four_tick_priority(t, my_ticks);
- //checking for
- //if true we have to write task 3 logic!
- // i mean as asked in hm timer_ticks() % TIMER_FREQ == 0 only thats when we have to to calculations.
- if (my_ticks % TIMER_FREQ == 0)
- {
- load_avg = fix_load_avg(load_avg, ready_threads);
- // here will go checking for reaaaadyy queueuee
- struct list_elem *elems;
- for (elems = list_begin(&all_list); elems != list_end(&all_list);)
- {
- struct thread *cur_thread = list_entry(elems, struct thread, elem);
- cur_thread->recent_cpu = fix_recent_cpu(load_avg, t->recent_cpu, t->nice);
- if (cur_thread->status == THREAD_READY || cur_thread->status == THREAD_RUNNING)
- {
- struct list_elem *tmp = elems;
- elems = list_next(elems);
- list_remove(tmp);
- cur_thread->priority = check_edge_priority(fix_int(fix_priority(PRI_MAX, cur_thread->recent_cpu, cur_thread->nice)));
- // printf('%d\n', cur_thread->priority);
- // list_push_front(&all_list, &t->elem);
- // if (cur_thread->status == THREAD_READY)
- // thread_unblock(cur_thread);
- list_arr_insert_pr(&cur_thread->elem, PRI_MAX, t->priority);
- }
- else
- {
- elems = list_next(elems);
- }
- }
- }
- }
- /* Update statistics. */
- if (t == idle_thread)
- idle_ticks++;
- #ifdef USERPROG
- else if (t->pagedir != NULL)
- user_ticks++;
- #endif
- else
- kernel_ticks++;
- /* Enforce preemption. */
- if (++thread_ticks >= TIME_SLICE)
- intr_yield_on_return();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement