1 /* Problem 1-4: Advanced Scheduler tests.
3 This depends on a correctly working Alarm Clock (Problem 1-1).
5 Run this test with and without the MLFQS enabled. The
6 threads' reported test should be better with MLFQS on than
7 with it off. You may have to tune the loop counts to get
10 Based on a test originally submitted for Stanford's CS 140 in
11 winter 1999 by by Matt Franklin
12 <startled@leland.stanford.edu>, Greg Hutchins
13 <gmh@leland.stanford.edu>, Yu Ping Hu <yph@cs.stanford.edu>.
14 Modified by arens and yph. */
16 /* Uncomment to print progress messages. */
17 /*#define SHOW_PROGRESS*/
19 #include "threads/test.h"
22 #include "threads/synch.h"
23 #include "threads/thread.h"
24 #include "devices/timer.h"
26 static thread_func io_thread;
27 static thread_func cpu_thread;
28 static thread_func io_cpu_thread;
33 static thread_func *funcs[] = {io_thread, cpu_thread, io_cpu_thread};
34 static const char *names[] = {"IO", "CPU", "IO & CPU"};
35 struct semaphore done[3];
40 "Testing multilevel feedback queue scheduler.\n");
43 for (i = 0; i < 3; i++)
45 sema_init (&done[i], 0, names[i]);
46 tids[i] = thread_create (names[i], PRI_DEFAULT, funcs[i], &done[i]);
49 /* Wait for threads to finish. */
50 for (i = 0; i < 3; i++)
52 #ifdef THREAD_JOIN_IMPLEMENTED
53 thread_join (tids[i]);
58 printf ("Multilevel feedback queue scheduler test done.\n");
62 cpu_thread (void *sema_)
64 struct semaphore *sema = sema_;
65 int64_t start = timer_ticks ();
69 lock_init (&lock, "cpu");
71 for (i = 0; i < 5000; i++)
75 printf ("CPU intensive: %d\n", thread_get_priority ());
80 printf ("CPU bound thread finished in %"PRId64" ticks.\n",
81 timer_elapsed (start));
87 io_thread (void *sema_)
89 struct semaphore *sema = sema_;
90 int64_t start = timer_ticks ();
93 for (i = 0; i < 1000; i++)
97 printf ("IO intensive: %d\n", thread_get_priority ());
101 printf ("IO bound thread finished in %"PRId64" ticks.\n",
102 timer_elapsed (start));
108 io_cpu_thread (void *sema_)
110 struct semaphore *sema = sema_;
112 int64_t start = timer_ticks ();
115 lock_init (&lock, "io & cpu");
117 for (i = 0; i < 800; i++)
123 for (j = 0; j < 15; j++)
125 lock_acquire (&lock);
127 printf ("Alternating IO/CPU: %d\n", thread_get_priority ());
129 lock_release (&lock);
133 printf ("Alternating IO/CPU thread finished in %"PRId64" ticks.\n",
134 timer_elapsed (start));