From: Ben Pfaff Date: Mon, 10 Apr 2006 00:28:22 +0000 (+0000) Subject: Invert the priority scheme, so that PRI_MIN is now the lowest priority X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pintos-anon;a=commitdiff_plain;h=225e6b43b823eec0f3eef093f697c0b62538344f Invert the priority scheme, so that PRI_MIN is now the lowest priority and PRI_MAX is now the highest priority. This should reduce student confusion (and at the same time subtly frustrate attempts to resubmit work from previous quarters). --- diff --git a/doc/44bsd.texi b/doc/44bsd.texi index 208dcd4..e442326 100644 --- a/doc/44bsd.texi +++ b/doc/44bsd.texi @@ -104,13 +104,13 @@ highest priority, yields. Our scheduler has 64 priorities and thus 64 ready queues, numbered 0 (@code{PRI_MIN}) through 63 (@code{PRI_MAX}). Lower numbers correspond -to @emph{higher} priorities, so that priority 0 is the highest priority -and priority 63 is the lowest. Thread priority is calculated initially +to lower priorities, so that priority 0 is the lowest priority +and priority 63 is the highest. Thread priority is calculated initially at thread initialization. It is also recalculated once every fourth clock tick, for every thread. In either case, it is determined by the formula -@center @t{@var{priority} = (@var{recent_cpu} / 4) + (@var{nice} * 2)}, +@center @t{@var{priority} = @code{PRI_MAX} - (@var{recent_cpu} / 4) - (@var{nice} * 2)}, @noindent where @var{recent_cpu} is an estimate of the CPU time the thread has used recently (see below) and @var{nice} is the thread's @@ -243,9 +243,10 @@ scheduler. It is not a complete description of scheduler requirements. Every thread has a @var{nice} value between -20 and 20 directly under its control. Each thread also has a priority, between 0 (@code{PRI_MIN}) through 63 (@code{PRI_MAX}), which is recalculated -using the following formula whenever the value of either term changes: +using the following formula whenever the value of either variable term +changes: -@center @t{@var{priority} = (@var{recent_cpu} / 4) + (@var{nice} * 2)}. +@center @t{@var{priority} = @code{PRI_MAX} - (@var{recent_cpu} / 4) - (@var{nice} * 2)}. @var{recent_cpu} measures the amount of CPU time a thread has received ``recently.'' On each timer tick, the running thread's @var{recent_cpu} diff --git a/doc/threads.texi b/doc/threads.texi index d1ff666..d83b52f 100644 --- a/doc/threads.texi +++ b/doc/threads.texi @@ -442,8 +442,8 @@ priority such that it no longer has the highest priority must cause it to immediately yield the CPU. Thread priorities range from @code{PRI_MIN} (0) to @code{PRI_MAX} (63). -Lower numbers correspond to @emph{higher} priorities, so that priority 0 -is the highest priority and priority 63 is the lowest. +Lower numbers correspond to lower priorities, so that priority 0 +is the lowest priority and priority 63 is the highest. The initial thread priority is passed as an argument to @func{thread_create}. If there's no reason to choose another priority, use @code{PRI_DEFAULT} (31). The @code{PRI_} macros are @@ -685,8 +685,8 @@ If multiple threads have the same highest priority, Priority donation only changes the priority of the donee thread. The donor thread's priority is unchanged. -Priority donation is not additive: if thread @var{A} (with priority 3) donates -to thread @var{B} (with priority 5), then @var{B}'s new priority is 3, not 8. +Priority donation is not additive: if thread @var{A} (with priority 5) donates +to thread @var{B} (with priority 3), then @var{B}'s new priority is 5, not 8. @item Can a thread's priority change while it is on the ready queue? diff --git a/doc/tour.texi b/doc/tour.texi index c1371cf..2e15b38 100644 --- a/doc/tour.texi +++ b/doc/tour.texi @@ -327,8 +327,8 @@ the page. @xref{Interrupt Handling}, for more information. @deftypecv {Member} {@struct{thread}} {int} priority A thread priority, ranging from @code{PRI_MIN} (0) to @code{PRI_MAX} -(63). Lower numbers correspond to @emph{higher} priorities, so that -priority 0 is the highest priority and priority 63 is the lowest. +(63). Lower numbers correspond to lower priorities, so that +priority 0 is the lowest priority and priority 63 is the highest. Pintos as provided ignores thread priorities, but you will implement priority scheduling in project 1 (@pxref{Priority Scheduling}). @end deftypecv diff --git a/src/tests/threads/alarm-priority.c b/src/tests/threads/alarm-priority.c index 51ca6a9..4839ecd 100644 --- a/src/tests/threads/alarm-priority.c +++ b/src/tests/threads/alarm-priority.c @@ -26,13 +26,13 @@ test_alarm_priority (void) for (i = 0; i < 10; i++) { - int priority = (i + 5) % 10 + PRI_DEFAULT + 1; + int priority = PRI_DEFAULT - (i + 5) % 10 - 1; char name[16]; snprintf (name, sizeof name, "priority %d", priority); thread_create (name, priority, alarm_priority_thread, NULL); } - thread_set_priority (PRI_MAX); + thread_set_priority (PRI_MIN); for (i = 0; i < 10; i++) sema_down (&wait_sema); diff --git a/src/tests/threads/alarm-priority.ck b/src/tests/threads/alarm-priority.ck index 6fa6128..c62b024 100644 --- a/src/tests/threads/alarm-priority.ck +++ b/src/tests/threads/alarm-priority.ck @@ -4,15 +4,15 @@ use warnings; use tests::tests; check_expected ([<<'EOF']); (alarm-priority) begin -(alarm-priority) Thread priority 32 woke up. -(alarm-priority) Thread priority 33 woke up. -(alarm-priority) Thread priority 34 woke up. -(alarm-priority) Thread priority 35 woke up. -(alarm-priority) Thread priority 36 woke up. -(alarm-priority) Thread priority 37 woke up. -(alarm-priority) Thread priority 38 woke up. -(alarm-priority) Thread priority 39 woke up. -(alarm-priority) Thread priority 40 woke up. -(alarm-priority) Thread priority 41 woke up. +(alarm-priority) Thread priority 30 woke up. +(alarm-priority) Thread priority 29 woke up. +(alarm-priority) Thread priority 28 woke up. +(alarm-priority) Thread priority 27 woke up. +(alarm-priority) Thread priority 26 woke up. +(alarm-priority) Thread priority 25 woke up. +(alarm-priority) Thread priority 24 woke up. +(alarm-priority) Thread priority 23 woke up. +(alarm-priority) Thread priority 22 woke up. +(alarm-priority) Thread priority 21 woke up. (alarm-priority) end EOF diff --git a/src/tests/threads/priority-change.c b/src/tests/threads/priority-change.c index 735920f..bb462d4 100644 --- a/src/tests/threads/priority-change.c +++ b/src/tests/threads/priority-change.c @@ -16,9 +16,9 @@ test_priority_change (void) ASSERT (!enable_mlfqs); msg ("Creating a high-priority thread 2."); - thread_create ("thread 2", PRI_DEFAULT - 1, changing_thread, NULL); + thread_create ("thread 2", PRI_DEFAULT + 1, changing_thread, NULL); msg ("Thread 2 should have just lowered its priority."); - thread_set_priority (PRI_DEFAULT + 2); + thread_set_priority (PRI_DEFAULT - 2); msg ("Thread 2 should have just exited."); } @@ -26,6 +26,6 @@ static void changing_thread (void *aux UNUSED) { msg ("Thread 2 now lowering priority."); - thread_set_priority (PRI_DEFAULT + 1); + thread_set_priority (PRI_DEFAULT - 1); msg ("Thread 2 exiting."); } diff --git a/src/tests/threads/priority-condvar.c b/src/tests/threads/priority-condvar.c index 51008ab..3e31081 100644 --- a/src/tests/threads/priority-condvar.c +++ b/src/tests/threads/priority-condvar.c @@ -24,10 +24,10 @@ test_priority_condvar (void) lock_init (&lock); cond_init (&condition); - thread_set_priority (PRI_MAX); + thread_set_priority (PRI_MIN); for (i = 0; i < 10; i++) { - int priority = (i + 7) % 10 + PRI_DEFAULT + 1; + int priority = PRI_DEFAULT - (i + 7) % 10 - 1; char name[16]; snprintf (name, sizeof name, "priority %d", priority); thread_create (name, priority, priority_condvar_thread, NULL); diff --git a/src/tests/threads/priority-condvar.ck b/src/tests/threads/priority-condvar.ck index d1d9038..580a019 100644 --- a/src/tests/threads/priority-condvar.ck +++ b/src/tests/threads/priority-condvar.ck @@ -4,35 +4,35 @@ use warnings; use tests::tests; check_expected ([<<'EOF']); (priority-condvar) begin -(priority-condvar) Thread priority 39 starting. -(priority-condvar) Thread priority 40 starting. -(priority-condvar) Thread priority 41 starting. -(priority-condvar) Thread priority 32 starting. -(priority-condvar) Thread priority 33 starting. -(priority-condvar) Thread priority 34 starting. -(priority-condvar) Thread priority 35 starting. -(priority-condvar) Thread priority 36 starting. -(priority-condvar) Thread priority 37 starting. -(priority-condvar) Thread priority 38 starting. +(priority-condvar) Thread priority 23 starting. +(priority-condvar) Thread priority 22 starting. +(priority-condvar) Thread priority 21 starting. +(priority-condvar) Thread priority 30 starting. +(priority-condvar) Thread priority 29 starting. +(priority-condvar) Thread priority 28 starting. +(priority-condvar) Thread priority 27 starting. +(priority-condvar) Thread priority 26 starting. +(priority-condvar) Thread priority 25 starting. +(priority-condvar) Thread priority 24 starting. (priority-condvar) Signaling... -(priority-condvar) Thread priority 32 woke up. +(priority-condvar) Thread priority 30 woke up. (priority-condvar) Signaling... -(priority-condvar) Thread priority 33 woke up. +(priority-condvar) Thread priority 29 woke up. (priority-condvar) Signaling... -(priority-condvar) Thread priority 34 woke up. +(priority-condvar) Thread priority 28 woke up. (priority-condvar) Signaling... -(priority-condvar) Thread priority 35 woke up. +(priority-condvar) Thread priority 27 woke up. (priority-condvar) Signaling... -(priority-condvar) Thread priority 36 woke up. +(priority-condvar) Thread priority 26 woke up. (priority-condvar) Signaling... -(priority-condvar) Thread priority 37 woke up. +(priority-condvar) Thread priority 25 woke up. (priority-condvar) Signaling... -(priority-condvar) Thread priority 38 woke up. +(priority-condvar) Thread priority 24 woke up. (priority-condvar) Signaling... -(priority-condvar) Thread priority 39 woke up. +(priority-condvar) Thread priority 23 woke up. (priority-condvar) Signaling... -(priority-condvar) Thread priority 40 woke up. +(priority-condvar) Thread priority 22 woke up. (priority-condvar) Signaling... -(priority-condvar) Thread priority 41 woke up. +(priority-condvar) Thread priority 21 woke up. (priority-condvar) end EOF diff --git a/src/tests/threads/priority-donate-multiple.c b/src/tests/threads/priority-donate-multiple.c index 6905691..e1bccbd 100644 --- a/src/tests/threads/priority-donate-multiple.c +++ b/src/tests/threads/priority-donate-multiple.c @@ -35,18 +35,18 @@ test_priority_donate_multiple (void) lock_acquire (&a); lock_acquire (&b); - thread_create ("a", PRI_DEFAULT - 1, a_thread_func, &a); + thread_create ("a", PRI_DEFAULT + 1, a_thread_func, &a); msg ("Main thread should have priority %d. Actual priority: %d.", - PRI_DEFAULT - 1, thread_get_priority ()); + PRI_DEFAULT + 1, thread_get_priority ()); - thread_create ("b", PRI_DEFAULT - 2, b_thread_func, &b); + thread_create ("b", PRI_DEFAULT + 2, b_thread_func, &b); msg ("Main thread should have priority %d. Actual priority: %d.", - PRI_DEFAULT - 2, thread_get_priority ()); + PRI_DEFAULT + 2, thread_get_priority ()); lock_release (&b); msg ("Thread b should have just finished."); msg ("Main thread should have priority %d. Actual priority: %d.", - PRI_DEFAULT - 1, thread_get_priority ()); + PRI_DEFAULT + 1, thread_get_priority ()); lock_release (&a); msg ("Thread a should have just finished."); diff --git a/src/tests/threads/priority-donate-multiple.ck b/src/tests/threads/priority-donate-multiple.ck index 9118722..d84a0ee 100644 --- a/src/tests/threads/priority-donate-multiple.ck +++ b/src/tests/threads/priority-donate-multiple.ck @@ -4,12 +4,12 @@ use warnings; use tests::tests; check_expected ([<<'EOF']); (priority-donate-multiple) begin -(priority-donate-multiple) Main thread should have priority 30. Actual priority: 30. -(priority-donate-multiple) Main thread should have priority 29. Actual priority: 29. +(priority-donate-multiple) Main thread should have priority 32. Actual priority: 32. +(priority-donate-multiple) Main thread should have priority 33. Actual priority: 33. (priority-donate-multiple) Thread b acquired lock b. (priority-donate-multiple) Thread b finished. (priority-donate-multiple) Thread b should have just finished. -(priority-donate-multiple) Main thread should have priority 30. Actual priority: 30. +(priority-donate-multiple) Main thread should have priority 32. Actual priority: 32. (priority-donate-multiple) Thread a acquired lock a. (priority-donate-multiple) Thread a finished. (priority-donate-multiple) Thread a should have just finished. diff --git a/src/tests/threads/priority-donate-nest.c b/src/tests/threads/priority-donate-nest.c index 1f24584..71cf8a9 100644 --- a/src/tests/threads/priority-donate-nest.c +++ b/src/tests/threads/priority-donate-nest.c @@ -43,15 +43,15 @@ test_priority_donate_nest (void) locks.a = &a; locks.b = &b; - thread_create ("medium", PRI_DEFAULT - 1, medium_thread_func, &locks); + thread_create ("medium", PRI_DEFAULT + 1, medium_thread_func, &locks); thread_yield (); msg ("Low thread should have priority %d. Actual priority: %d.", - PRI_DEFAULT - 1, thread_get_priority ()); + PRI_DEFAULT + 1, thread_get_priority ()); - thread_create ("high", PRI_DEFAULT - 2, high_thread_func, &b); + thread_create ("high", PRI_DEFAULT + 2, high_thread_func, &b); thread_yield (); msg ("Low thread should have priority %d. Actual priority: %d.", - PRI_DEFAULT - 2, thread_get_priority ()); + PRI_DEFAULT + 2, thread_get_priority ()); lock_release (&a); thread_yield (); @@ -69,7 +69,7 @@ medium_thread_func (void *locks_) lock_acquire (locks->a); msg ("Medium thread should have priority %d. Actual priority: %d.", - PRI_DEFAULT - 2, thread_get_priority ()); + PRI_DEFAULT + 2, thread_get_priority ()); msg ("Medium thread got the lock."); lock_release (locks->a); diff --git a/src/tests/threads/priority-donate-nest.ck b/src/tests/threads/priority-donate-nest.ck index 597d99d..6ec1b4a 100644 --- a/src/tests/threads/priority-donate-nest.ck +++ b/src/tests/threads/priority-donate-nest.ck @@ -4,9 +4,9 @@ use warnings; use tests::tests; check_expected ([<<'EOF']); (priority-donate-nest) begin -(priority-donate-nest) Low thread should have priority 30. Actual priority: 30. -(priority-donate-nest) Low thread should have priority 29. Actual priority: 29. -(priority-donate-nest) Medium thread should have priority 29. Actual priority: 29. +(priority-donate-nest) Low thread should have priority 32. Actual priority: 32. +(priority-donate-nest) Low thread should have priority 33. Actual priority: 33. +(priority-donate-nest) Medium thread should have priority 33. Actual priority: 33. (priority-donate-nest) Medium thread got the lock. (priority-donate-nest) High thread got the lock. (priority-donate-nest) High thread finished. diff --git a/src/tests/threads/priority-donate-one.c b/src/tests/threads/priority-donate-one.c index 603a7fe..2a67b52 100644 --- a/src/tests/threads/priority-donate-one.c +++ b/src/tests/threads/priority-donate-one.c @@ -31,12 +31,12 @@ test_priority_donate_one (void) lock_init (&lock); lock_acquire (&lock); - thread_create ("acquire1", PRI_DEFAULT - 1, acquire1_thread_func, &lock); + thread_create ("acquire1", PRI_DEFAULT + 1, acquire1_thread_func, &lock); msg ("This thread should have priority %d. Actual priority: %d.", - PRI_DEFAULT - 1, thread_get_priority ()); - thread_create ("acquire2", PRI_DEFAULT - 2, acquire2_thread_func, &lock); + PRI_DEFAULT + 1, thread_get_priority ()); + thread_create ("acquire2", PRI_DEFAULT + 2, acquire2_thread_func, &lock); msg ("This thread should have priority %d. Actual priority: %d.", - PRI_DEFAULT - 2, thread_get_priority ()); + PRI_DEFAULT + 2, thread_get_priority ()); lock_release (&lock); msg ("acquire2, acquire1 must already have finished, in that order."); msg ("This should be the last line before finishing this test."); diff --git a/src/tests/threads/priority-donate-one.ck b/src/tests/threads/priority-donate-one.ck index ff019a2..c09ecfd 100644 --- a/src/tests/threads/priority-donate-one.ck +++ b/src/tests/threads/priority-donate-one.ck @@ -4,8 +4,8 @@ use warnings; use tests::tests; check_expected ([<<'EOF']); (priority-donate-one) begin -(priority-donate-one) This thread should have priority 30. Actual priority: 30. -(priority-donate-one) This thread should have priority 29. Actual priority: 29. +(priority-donate-one) This thread should have priority 32. Actual priority: 32. +(priority-donate-one) This thread should have priority 33. Actual priority: 33. (priority-donate-one) acquire2: got the lock (priority-donate-one) acquire2: done (priority-donate-one) acquire1: got the lock diff --git a/src/tests/threads/priority-fifo.c b/src/tests/threads/priority-fifo.c index e71b7e5..2f89196 100644 --- a/src/tests/threads/priority-fifo.c +++ b/src/tests/threads/priority-fifo.c @@ -50,7 +50,7 @@ test_priority_fifo (void) ASSERT (output != NULL); lock_init (&lock); - thread_set_priority (PRI_DEFAULT - 2); + thread_set_priority (PRI_DEFAULT + 2); for (i = 0; i < THREAD_CNT; i++) { char name[16]; @@ -60,7 +60,7 @@ test_priority_fifo (void) d->iterations = 0; d->lock = &lock; d->op = &op; - thread_create (name, PRI_DEFAULT - 1, simple_thread_func, d); + thread_create (name, PRI_DEFAULT + 1, simple_thread_func, d); } thread_set_priority (PRI_DEFAULT); diff --git a/src/tests/threads/priority-preempt.c b/src/tests/threads/priority-preempt.c index 452c680..70f8acb 100644 --- a/src/tests/threads/priority-preempt.c +++ b/src/tests/threads/priority-preempt.c @@ -23,7 +23,7 @@ test_priority_preempt (void) /* Make sure our priority is the default. */ ASSERT (thread_get_priority () == PRI_DEFAULT); - thread_create ("high-priority", PRI_DEFAULT - 1, simple_thread_func, NULL); + thread_create ("high-priority", PRI_DEFAULT + 1, simple_thread_func, NULL); msg ("The high-priority thread should have already completed."); } diff --git a/src/tests/threads/priority-sema.c b/src/tests/threads/priority-sema.c index 8ca17a0..d7eb9cc 100644 --- a/src/tests/threads/priority-sema.c +++ b/src/tests/threads/priority-sema.c @@ -21,10 +21,10 @@ test_priority_sema (void) ASSERT (!enable_mlfqs); sema_init (&sema, 0); - thread_set_priority (PRI_MAX); + thread_set_priority (PRI_MIN); for (i = 0; i < 10; i++) { - int priority = (i + 3) % 10 + PRI_DEFAULT + 1; + int priority = PRI_DEFAULT - (i + 3) % 10 - 1; char name[16]; snprintf (name, sizeof name, "priority %d", priority); thread_create (name, priority, priority_sema_thread, NULL); diff --git a/src/tests/threads/priority-sema.ck b/src/tests/threads/priority-sema.ck index eee1de6..f2b9cde 100644 --- a/src/tests/threads/priority-sema.ck +++ b/src/tests/threads/priority-sema.ck @@ -4,25 +4,25 @@ use warnings; use tests::tests; check_expected ([<<'EOF']); (priority-sema) begin -(priority-sema) Thread priority 32 woke up. +(priority-sema) Thread priority 30 woke up. (priority-sema) Back in main thread. -(priority-sema) Thread priority 33 woke up. +(priority-sema) Thread priority 29 woke up. (priority-sema) Back in main thread. -(priority-sema) Thread priority 34 woke up. +(priority-sema) Thread priority 28 woke up. (priority-sema) Back in main thread. -(priority-sema) Thread priority 35 woke up. +(priority-sema) Thread priority 27 woke up. (priority-sema) Back in main thread. -(priority-sema) Thread priority 36 woke up. +(priority-sema) Thread priority 26 woke up. (priority-sema) Back in main thread. -(priority-sema) Thread priority 37 woke up. +(priority-sema) Thread priority 25 woke up. (priority-sema) Back in main thread. -(priority-sema) Thread priority 38 woke up. +(priority-sema) Thread priority 24 woke up. (priority-sema) Back in main thread. -(priority-sema) Thread priority 39 woke up. +(priority-sema) Thread priority 23 woke up. (priority-sema) Back in main thread. -(priority-sema) Thread priority 40 woke up. +(priority-sema) Thread priority 22 woke up. (priority-sema) Back in main thread. -(priority-sema) Thread priority 41 woke up. +(priority-sema) Thread priority 21 woke up. (priority-sema) Back in main thread. (priority-sema) end EOF diff --git a/src/threads/thread.c b/src/threads/thread.c index 16b8c8b..ca8a8b0 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -92,7 +92,7 @@ thread_init (void) void thread_start (void) { - thread_create ("idle", PRI_MAX, idle, NULL); + thread_create ("idle", PRI_MIN, idle, NULL); intr_enable (); } diff --git a/src/threads/thread.h b/src/threads/thread.h index 2043d8d..fbef34b 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -20,9 +20,9 @@ typedef int tid_t; #define TID_ERROR ((tid_t) -1) /* Error value for tid_t. */ /* Thread priorities. */ -#define PRI_MIN 0 /* Highest priority. */ +#define PRI_MIN 0 /* Lowest priority. */ #define PRI_DEFAULT 31 /* Default priority. */ -#define PRI_MAX 63 /* Lowest priority. */ +#define PRI_MAX 63 /* Highest priority. */ /* A kernel thread or user process.