Open mywnajsldkf opened 1 year ago
pintos/threads
์์ make check
๋ฅผ ์คํํ๋ค.
๋ถ๋ถ๋ง ํ
์คํธํ๊ณ ์ถ๋ค๋ฉด,
pintos/threads/build
์์ ๋ค์๊ณผ ๊ฐ์ด ์คํํ๋ค.
pintos -v -k -T 60 -m 20 -- -q run priority-fifo < /dev/null 2> tests/threads/priority-fifo.errors > tests/threads/priority-fifo.output
perl -I../.. ../../tests/threads/priority-fifo.ck tests/threads/priority-fifo tests/threads/priority-fifo.result
์ฒซ๋ฒ์งธ ์๋์์ priority-codvar ํ ์คํธ ํต๊ณผ๋ฅผ ํ์ง ๋ชปํ๋๋ฐ, ํ์ธํด๋ณด๋ ์ง์ ๊ตฌํํ cmp_sem_priority ๋ฌธ์ ์๋ค. list_front ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ธ๋งํฌ์ด wait list์ ์ฒซ๋ฒ์งธ ์ค๋ ๋๋ฅผ ํ์ธํ๋๋ฐ, ๋ง์ฝ wait list์ ์๋ฌด๊ฒ๋ ์๋ค๋ฉด list_front์์ list๊ฐ ๋น์ด์๋์ง ํ์ธํ๋ assert์ ๊ฑธ๋ฆด ๊ฒ์ด๋ค! ๋ง์ฝ list_begin ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ด ๋ถ๋ถ์ ๊ฑธ๋ฆฌ์ง ์์ํ ๋ฐ... ํ ,,, ์ ๋ ํจ์๋ฅผ ๊ตฌ๋ถํด๋์๋์ง ์ ๋ชจ๋ฅด๊ฒ ๋ค. ๐ง ์ผ๋จ list_empty ํจ์๋ฅผ ์ด์ฉํด ๋น์๋์ง ํ์ธํ๋ ์์ผ๋ก ์์ ํ๋ ํต๊ณผ๋์๋ค.
struct list_elem *
list_front (struct list *list) {
ASSERT (!list_empty (list));
return list->head.next;
}
bool
cmp_sem_priority (const struct list_elem *a, const struct list_elem *b, void *aux) {
struct semaphore_elem *sa = list_entry(a, struct semaphore_elem, elem);
struct semaphore_elem *sb = list_entry(b, struct semaphore_elem, elem);
if (!list_empty(&(sa->semaphore.waiters)) && !list_empty(&(sb->semaphore.waiters)))
{
struct thread *a_thread = list_entry(list_front(&(sa->semaphore.waiters)), struct thread, elem);
struct thread *b_thread = list_entry(list_front(&(sb->semaphore.waiters)), struct thread, elem);
return a_thread->priority > b_thread->priority;
}
}
์ด ์ฝ๋์ ๋ฌธ์ ๊ฐ ์์๊น์? ๋ต์ ์์ต๋๋ค...
๊ทผ๋ฐ ์ ์๋์๊ฐ๊ณ , ๊ณ์ list_elem(semaphore_elem)์ ์ฐพ์ ์ ์๋ค๊ณ ์๋ฌ์ฝ๋๋ ๋์ง๊ณ ์๋ ๊ฒ์ผ๊น์??
์ผ๋จ,,, ๋ค๋ฅธ ์กฐ์์ด ์์ฑํ ๊ฒ์ฒ๋ผ ํด๋น ํจ์๋ฅผ ์์ค ์ฝ๋ ๊ฐ์ฅ ๋ฐ์ผ๋ก ์ด๋์์ผฐ์ต๋๋ค.
์ ์ผ๋จ ๋์ง๋ง ์ฐ์ฐํฉ๋๋ค...
๋ฌธ์ ๊ฐ ๋ฌด์์ผ๊น์??
๊ทธ๊ฒ์ ๋ฐ๋ก....
// ์ฝ 160์ค
bool
cmp_sem_priority (const struct list_elem *a, const struct list_elem *b, void *aux) {
struct semaphore_elem *sa = list_entry(a, struct semaphore_elem, elem);
struct semaphore_elem *sb = list_entry(b, struct semaphore_elem, elem);
if (!list_empty(&(sa->semaphore.waiters)) && !list_empty(&(sb->semaphore.waiters)))
{
struct thread *a_thread = list_entry(list_front(&(sa->semaphore.waiters)), struct thread, elem);
struct thread *b_thread = list_entry(list_front(&(sb->semaphore.waiters)), struct thread, elem);
return a_thread->priority > b_thread->priority;
}
}
...
// ๋๋ต 250์ค์์ semaphore_elem์ ๋ง๋ ์ ์์์ต๋๋ค...
/* One semaphore in a list. */
struct semaphore_elem {
struct list_elem elem; /* List element. */
struct semaphore semaphore; /* This semaphore. */
};
์๊ณ ๋ณด๋! semaphore_elem ๊ตฌ์กฐ์ฒด ์ ์ธ ์ ์ semaphore_elem์ ์ฐพ๊ณ ์์๋ค์... ํ ,,, ์ ์ด๋ ๊ฒ ์ฝ๋๋ฅผ ์์ฑํ์๊น์? header ํ์ผ๋ก ์ด๋ํด๋ ์ข์ ๊ฒ ๊ฐ์๋ฐ ๋ง์ด์ฃ ... ์๋๊ฐ ์์๊น์?
์ํผ ์ด๊ฒ์ ํด๊ฒฐํ๋๋ฐ 1์๊ฐ 30๋ถ์ด ๊ฑธ๋ ธ์ต๋๋ค...
feat. ๊ฐ์ด ๋จธ๋ฆฌ๋ฅผ ํจ๊ป ๋ชจ์์ค,,, 99์ฆ ํ๋ ์ฆ Thanks a lot!๐ @KimCookieYa @chwangmin
7. Priority Scheduling ๐
โ ๊ณผ์ ๋ฏธ์
๐พ ์์
Priority Scheduling
void test_max_priority (void)
ํ์ฌ ์ํ์ค์ธ ์ค๋ ๋์ ๊ฐ์ฅ ๋์ ์ฐ์ ์์์ ์ค๋ ๋์ ์ฐ์ ์์๋ฅผ ๋น๊ตํ์ฌ ์ค์ผ์ฅด๋งํ๋ค.bool cmp_priority(const struct list_elem *a, const struct list_elem *b, void *aux UNUSED
์ธ์๋ก ์ฃผ์ด์ง ์ค๋ ๋๋ค์ ์ฐ์ ์์๋ฅผ ๋น๊ตํ๋ค.thread_create
์์ฑ๋ ์ค๋ ๋์ ์ฐ์ ์์๊ฐ ํ์ฌ ์คํ์ค์ธ ์ค๋ ๋์ ์ฐ์ ์์๋ณด๋ค ๋์ผ๋ฉด CPU๋ฅผ ์๋ณดํ๋ค.void thread_unblock (struct thread *t)
์ค๋ ๋๊ฐ unblock๋ ๋ ์ฐ์ ์์ ์์ผ๋ก ์ ๋ ฌ๋์ด ready_list์ ์ฝ์ ๋๋ค.void thread_yield(void)
thread๊ฐ CPU๋ฅผ ์๋ณดํ์ฌ ready_list์ ์ฝ์ ๋ ๋ ์ฐ์ ์์ ์์๋ก ์ ๋ ฌ๋์ด ์ฝ์ ๋๋๋ก ์์ ํ๋ค.void thread_set_priority (int new_priority)
์ค๋ ๋์ ์ฐ์ ์์๊ฐ ๋ณ๊ฒฝ๋์์ ๋ ์ฐ์ ์์์ ๋ฐ๋ผ ์ ์ ์ด ๋ฐ์ํ๋ค.bool cmp_priority (construct struct list_elem* a_, const struct list_elem* b_, void* aux UNUSED)
์ฒซ๋ฒ์งธ ์ธ์์ ๋๋ฒ์งธ ์ธ์๋ฅผ ๋น๊ตํ์ฌ ์ ๋ ฌํ๊ธฐ ์ํ ๊ธฐ์ค์ ์์ฑํ๋ค.โญ๏ธ ์๊ฐํ ์
8. Priority Scheduling and Synchronization
โ ๊ณผ์ ๋ฏธ์
๐พ ์์
Priority Scheduling-Synchronization
void sema_down (struct semaphore *sema)
์ธ๋งํฌ๋ฅผ ํ๋ํ๊ณ waiters ๋ฆฌ์คํธ์ ์ฐ์ ์์๋๋ก ์ฝ์ ํ๋ค.void sema_up (struct semaphore *sema)
์ค๋ ๋๊ฐ waiters list์ ์๋ ๋์ ์ฐ์ ์์๊ฐ ๋ณ๊ฒฝ๋์์ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ์ฌ, waiters list๋ฅผ ์ฐ์ ์์๋ก ์ ๋ ฌํ๋ค.void cond_wait (struct condition *cont, struct lock *lock)
condition variable์ waiters list์ ์ฐ์ ์์ ์์๋ก ์ฝ์ ํ๋ค.void cond_signal(struct condition *cond, struct lock *lock UNUSED)
condition variable์ waiters list๋ฅผ ์ฐ์ ์์๋ก ์ฌ์ ๋ ฌํ๋ค. (๋๊ธฐ์ค์ ์ฐ์ ์์๊ฐ ๋ณ๊ฒฝ๋์์ ๊ฒฝ์ฐ ๊ณ ๋ ค!)bool cmp_sem_priority(const struct list_elem *a, const struct list_elem *b, void *aux UNUSED)
์ธ๋งํฌ์ด ๋ฆฌ์คํธ๋ฅผ ๋์ ์ฐ์ ์์๋ฅผ ๊ฐ์ง๋ ์ค๋ ๋์ ์ฐ์ ์์ ์์ผ๋ก ์ ๋ ฌํ๋ค.โญ๏ธ ์๊ฐํ ์