PintOS-jungle) alarm clock

Mongle·2021년 2월 2일
0

OS프로젝트

목록 보기
3/8

PintOS project01-1) alarm clock

문제 상황

기존의 pintos 코드는 running queue에 thread가 들어오면 실행할 tick이 아니어도 CPU를 점유할 차례가 오면 실행되었다가 실행할 때가 아닌 것을 확인하고 다시 ready queue에 들어가는 일을 반복해야한다. 이를 busy waiting 이라고 한다.

Busy waiting


문제점
실행될 수 있기까지 많은 시간이 필요한 경우, 실행 가능한지 확인하기위해 CPU를 점유했다가 다시 ready list에 들어가는 작업을 반복하기 때문에 CPU를 낭비하게 된다

해결방안 : sleeping
해당 스레드가 권한을 얻기 위해 기다리는 시간을 저장해놓고, sleep list에 실행 중인 Thread가 자신이 깨야할 시각을 가지고 잠들어있게 한다. 불필요한 CPU 점유를 막을 수 있다.

주요 함수

void thread_sleep(int64_t ticks)
{
    struct thread *curr = thread_current();
    enum intr_level old_level;

    ASSERT(!intr_context());

    old_level = intr_disable();

    if (curr != idle_thread)
    {
        curr->wakeup_tick = ticks;
        list_push_back(&sleep_list, &curr->elem);
        update_next_tick_to_awake(curr->wakeup_tick);
        thread_block();
    }
    // do_schedule (THREAD_BLOCKED);
    intr_set_level(old_level);
}

// ticks는 절대 시간
void thread_awake(int64_t ticks)
{
    next_tick_to_awake = INT64_MAX;
    struct list_elem *e = list_begin(&sleep_list);
    while (e != list_tail(&sleep_list))
    {
        struct thread *curr = list_entry(e, struct thread, elem);
        if (curr->wakeup_tick <= ticks)
        {
            e = list_remove(e);
            thread_unblock(curr);
        }else
        {
            update_next_tick_to_awake(curr->wakeup_tick);
            e = list_next(e);
        }
    }
}

전체 코드 : Git repository

profile
https://github.com/Jeongseo21

0개의 댓글