1 /* Problem 1-3: Priority Scheduling tests.
3 Based on a test originally submitted for Stanford's CS 140 in
4 winter 1999 by by Matt Franklin
5 <startled@leland.stanford.edu>, Greg Hutchins
6 <gmh@leland.stanford.edu>, Yu Ping Hu <yph@cs.stanford.edu>.
10 #error This test not applicable with MLFQS enabled.
13 #include "threads/test.h"
15 #include "threads/synch.h"
16 #include "threads/thread.h"
18 static void test_preempt (void);
19 static void test_fifo (void);
20 static void test_donate_return (void);
25 /* Make sure our prority is the default. */
26 ASSERT (thread_get_priority () == PRI_DEFAULT);
30 test_donate_return ();
33 static thread_func simple_thread_func;
34 static thread_func acquire_thread_func;
40 "Testing priority preemption.\n");
41 thread_create ("high-priority", PRI_DEFAULT + 1, simple_thread_func, NULL);
42 printf ("The high-priority thread should have already completed.\n"
43 "Priority preemption test done.\n");
52 "Testing FIFO preemption.\n"
53 "5 threads will iterate 10 times in the same order each time.\n"
54 "If the order varies then there is a bug.\n");
56 thread_set_priority (PRI_DEFAULT + 2);
57 for (i = 0; i < 10; i++)
60 snprintf (name, sizeof name, "%d", i);
61 thread_create (name, PRI_DEFAULT + 1, simple_thread_func, NULL);
63 thread_set_priority (PRI_DEFAULT);
65 printf ("FIFO preemption test done.\n");
69 test_donate_return (void)
74 "Testing priority donation.\n"
75 "If the statements printed below are all true, you pass.\n");
77 lock_init (&lock, "donor");
79 thread_create ("acquire1", PRI_DEFAULT + 1, acquire_thread_func, &lock);
80 printf ("This thread should have priority %d. Actual priority: %d.\n",
81 PRI_DEFAULT + 1, thread_get_priority ());
82 thread_create ("acquire2", PRI_DEFAULT + 2, acquire_thread_func, &lock);
83 printf ("This thread should have priority %d. Actual priority: %d.\n",
84 PRI_DEFAULT + 2, thread_get_priority ());
86 printf ("acquire2 and acquire1 must already have finished, in that order.\n"
87 "This should be the last line before finishing this test.\n"
88 "Priority donation test done.\n");
92 simple_thread_func (void *aux UNUSED)
96 for (i = 0; i < 5; i++)
98 printf ("Thread %s iteration %d\n", thread_name (), i);
101 printf ("Thread %s done!\n", thread_name ());
105 acquire_thread_func (void *lock_)
107 struct lock *lock = lock_;
110 printf ("%s: got the lock\n", thread_name ());
112 printf ("%s: done\n", thread_name ());