[PINTOS_PROJECT1] PRIORITY SCHEDULING

zeo·2021년 10월 4일
0

ready_list 에 push 할 때 priority 순서에 맞추어 push하여, 우선순위가 높은 스레드 먼저 실행될 수 있도록 구현

1. cmp_thread_priority()

// pintos project - priority
// 스레드 2개를 인자로 받아, 각각의 우선순위를 비교하는 함수
// list_insert_ordered()에 사용됨
// a의 우선순위가 높으면 1(true), b의 우선순위가 높으면 0(false)을 리턴
// UNUSED 는, unused error 피하기 위한 설정
bool
cmp_thread_priority(const struct list_elem *a, const struct list_elem *b, void *aux UNUSED) {
	struct thread *thread_a = list_entry(a, struct thread, elem);
	struct thread *thread_b = list_entry(b, struct thread, elem);

	if(thread_a != NULL && thread_b != NULL) {
		if(thread_a->priority > thread_b->priority) return true;
		else return false;
	}
	return false;
}

2. test_max_priority()

// pintos project - priority
// running 스레드와, ready_list의 가장 앞의 스레드의 priority 비교
void
test_max_priority(void) {
	struct thread *cp = thread_current(); // 현재 실행중인 스레드
	struct thread *first_thread; 

	if(list_empty(&ready_list)) return;

	// ready_list의 첫번째 스레드(즉, list 내 가장 높은 우선순위를 가진 스레드)
	// ready_list의 삽입 방식을 priority 값에 따라 정렬하여 넣는 것으로 수정했으므로
	first_thread = list_entry(list_front(&ready_list), struct thread, elem);

	// 현재 실행중인 스레드의 우선순위가, ready_list의 첫번째 스레드의 우선순위 보다 낮은 우선순위를 가지면
	// thread_yield()를 통해 cpu 양보
	if(cp->priority < first_thread -> priority)
		thread_yield();
}

0개의 댓글