cd threads # cd = change directory
make
# 잘 되었는지 확인
make check
# test 프로그램이 돈 후 다음 message 가 나오면 정상
# 20 of 27 tests failed.
• timer_sleep(int64_t ticks)
→ 현재 스레드를 주어진 시간(ticks)만큼 잠재웠다가, 시간이 지나면 자동으로 깨어나도록 구현
• 기존 busy-waiting 방식 → 효율적인 sleep/wakeup 방식으로 변경
⸻
timer_sleep()의 기존 구현 (in timer.c)
while (timer_elapsed(start) < ticks)
thread_yield(); // 계속 CPU 사용 → 비효율
• 문제점: CPU를 낭비하면서 반복적으로 조건 확인
• 해결 방향: 스레드를 BLOCKED 상태로 전환하여 sleep list에 넣고, 타이머 인터럽트가 발생할 때 다시 깨움
⸻

변경 대상 파일
•/timer.c
•threads/thread.c, threads/thread.h
⸻
static struct list sleep_list;
int64_t wakeup_tick;
⸻
[1] timer_sleep(int64_t ticks)
if (남은 시간 > 0)
thread_sleep(현재 시간 + ticks);
void
timer_sleep (int64_t ticks) {
int64_t start = timer_ticks ();
ASSERT (intr_get_level () == INTR_ON);
// while (timer_elapsed (start) < ticks)
// thread_yield ();
if (timer_elapsed(start)<ticks){
thread_sleep(start+ticks);
}
}
⸻
[2] thread_sleep(int64_t wakeup_tick)
• 현재 스레드 상태를 BLOCKED로 설정
• wakeup_tick 저장
• sleep_list에 정렬된 상태로 삽입 (오름차순)
• schedule() 호출 (다른 스레드에게 CPU 넘김)
주의: sleep_list 접근 시 인터럽트 반드시 비활성화!
⸻
[3] timer_interrupt()
• 매 tick마다 호출되는 인터럽트 핸들러
static void
timer_interrupt (struct intr_frame *args UNUSED) {
ticks++;
thread_tick ();
/*check sleep list and the global tick
find any thread to wake up
move them to the ready list if necessary
update the global tick
*/
}
⸻
구현 체크리스트
✅ sleep_list 선언 및 초기화 thread.c에서 전역으로 선언, thread_init()에서 초기화
✅ wakeup_tick 필드 추가 struct thread에 int64_t wakeup_tick 추가
✅ thread_sleep() 구현 BLOCKED 상태 전환 + 리스트 삽입 + schedule() 호출
✅ 정렬 기준 함수 작성 list_insert_ordered()용 비교 함수 필요
✅ timer_interrupt() 수정 매 tick마다 sleep_list 앞쪽부터 확인 및 깨우기
✅ 인터럽트 제어 리스트 조작 시 반드시 intr_disable() / intr_set_level() 사용
⸻
• list_insert_ordered(&sleep_list, &t->elem, wakeup_compare, NULL);
→ wakeup_compare는 wake_tick 기준 오름차순 비교 함수
• 전역 wakeup_tick 최소값을 유지해두면 인터럽트 최적화 가능
⸻
timer_sleep(ticks) 현재 스레드를 ticks만큼 재움
thread_sleep(wakeup_tick) BLOCKED 전환 후 sleep_list에 삽입
timer_interrupt() 깰 시간 된 스레드를 깨움 (thread_unblock)
⸻
쉽네 ㅋ