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/malloc.h"
16 #include "threads/synch.h"
17 #include "threads/thread.h"
19 static void test_fifo (void);
24 /* Make sure our priority is the default. */
25 ASSERT (thread_get_priority () == PRI_DEFAULT);
30 static thread_func simple_thread_func;
32 struct simple_thread_data
34 struct lock *lock; /* Lock on output. */
35 char **out; /* Output pointer. */
41 struct simple_thread_data data;
47 "Testing FIFO preemption.\n"
48 "10 threads will iterate 5 times in the same order each time.\n"
49 "If the order varies then there is a bug.\n");
51 output = cp = malloc (5 * 10 * 128);
52 ASSERT (output != NULL);
53 lock_init (&lock, "output");
58 thread_set_priority (PRI_DEFAULT + 2);
59 for (i = 0; i < 10; i++)
62 snprintf (name, sizeof name, "%d", i);
63 thread_create (name, PRI_DEFAULT + 1, simple_thread_func, &data);
65 thread_set_priority (PRI_DEFAULT);
69 printf ("%sFIFO preemption test done.\n", output);
74 simple_thread_func (void *data_)
76 struct simple_thread_data *data = data_;
79 for (i = 0; i < 5; i++)
81 lock_acquire (data->lock);
82 *data->out += snprintf (*data->out, 128, "Thread %s iteration %d\n",
84 lock_release (data->lock);
88 lock_acquire (data->lock);
89 *data->out += snprintf (*data->out, 128,
90 "Thread %s done!\n", thread_name ());
91 lock_release (data->lock);