운영체제

PintOS - Busy Waiting 바쁜대기 - 2

whh1323 2020. 1. 22. 16:04

/* Sleeps for approximately TICKS timer ticks.  Interrupts must
   be turned on. */
void
timer_sleep (int64_t ticks)
{
  int64_t start = timer_ticks ();

  ASSERT (intr_get_level () == INTR_ON);
  while (timer_elapsed (start) < ticks)
    thread_yield ();

 

thread_yield ();는 무엇을 하는 얘일까?

 

 

-->/pintos/src/threads/thread.c<---
/* Yields the CPU.  The current thread is not put to sleep and
   may be scheduled again immediately at the scheduler's whim. */
void
thread_yield (void)
{
  struct thread *cur = thread_current ();
  enum intr_level old_level;

  ASSERT (!intr_context ());

  old_level = intr_disable ();
  if (cur != idle_thread)
    list_push_back (&ready_list, &cur->elem);
  cur->status = THREAD_READY;
  schedule ();
  intr_set_level (old_level);
}

현재 점유상태인 쓰레드를 ready_list (레디큐 맨 뒤)로 보내버리고 다른 쓰레드에게 양보합니다.

 

 

---> /src/devices/timer.c <---

/* Sleeps for approximately TICKS timer ticks.  Interrupts must
   be turned on. */
void
timer_sleep (int64_t ticks)
{
  int64_t start = timer_ticks ();

  ASSERT (intr_get_level () == INTR_ON);
  while (timer_elapsed (start) < ticks)
    thread_yield ();
}

위에 thread_yield 와 timer_sleep를 보아 시간을  반복문을 통하여 계속 잰 후에

다른 쓰레드에게 양보하는 모습을 보입니다.

 

자 이제 반복문을 사용하면서 시간을 체크하는 대신에 시간을 미리 체크해 두고

그 시간동안 Thread를 재워두고 시간이 되면 Ready 큐에 삽입해 주는 방법 (sleep-wakeup)방식으로

구현을 해보겠습니다.