
2025.05.12
오늘한 내용 : PintOS - Project1: Threads - Alarm Clock 구현
WEEK 09 : 정글 끝까지(PintOS) - Threads
devices/timer.c)timer_sleep() 재구현void
timer_sleep (int64_t ticks) {
int64_t start = timer_ticks ();
ASSERT (intr_get_level () == INTR_ON);
while (timer_elapsed (start) < ticks)
thread_yield ();
}
build 폴더 내부에서
make tests/threads/(테스트 하고싶은 테스트 명).result VERBOSE=1
ex)
make tests/threads/alarm-simultaneous.result VERBOSE=1
[NEW]
│ thread_create()
▼
[READY] ←––– thread_unblock() 호출
│ schedule()
▼
[RUNNING]
│ thread_block() 호출 (lock_fail, cond_wait, timer_sleep 등)
▼
[BLOCKED] ←––– event 발생 시 thread_unblock()
│
└───────────(thread_unblock)───▶ back to [READY]
그리고 실행이 다시 잡히면
[READY] –– schedule() ––▶ [RUNNING]
thread_create() 로 생성 직후 바로 준비 큐로schedule())가 선택해 문맥 전환lock_acquire() 실패 → thread_block()cond_wait() → thread_block()timer_sleep() → thread_block()lock_release() / sema_up()cond_signal() / cond_broadcast()timer_interrupt() 에서 슬립 리스트 처리thread_unblock() 호출해 준비 큐로 복귀return 또는 thread_exit() → 자원 정리 → 더 이상 READY/RUNNING 상태로 돌아오지 않음이 사이클을 통해 “준비 큐에 들어가고 → Running → Blocked → (Unblock) → 다시 Ready → Running” 과 같은 흐름을 거침
timer_sleep()을 busy‐wait 대신 다른 방식으로 재구현devices/timer.cvoid timer_sleep(int64_t ticks)(wake_tick, cur_thread)thread_block()timer_interrupt()current_ticks++ → 슬립 리스트 순회 → 기상 시점 도달 스레드 thread_unblock()* thread.h
int64_t wakeup_tick; 추가
struct list_elem sleep_elem; 추가
----------------------------------------
* timer.c
list.c 참고
sleep_list 선언
timer_init() - sleep_list 초기화 추가
timer_sleep() - busy_wait 제거
thread_current(),list_insert_ordered() 사용
wakeup_tick_less() 구현
list_entry() - elem포인터가 있는 상위 구조체를 반환
timer_interupt() - 깨우기 구현
struct thread
{
/*... 기존 필드...*/
/* alarm을 위한 깨울 시각, 슬립 리스트 연결용 엘리먼트 추가*/
int64_t wakeup_tick;
struct list_elem sleep_elem; //list.h에 자료구조 있음
};
timer_sleep() 호출 시 깰 시각, 리스트에 추가(b 방법사용)thread_current()->wakeup_tick = start + ticks;list.c 참고list_insert_ordered()가 O(n) (한 번만)timer_interrupt()tick보다 list 맨 앞의 wakeup_tick이 작으면 unblockwakeup_tick이 현재 tick 보다 크면 break