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
9 Based on a test originally submitted for Stanford's CS 140 in
10 winter 1999 by by Matt Franklin
11 <startled@leland.stanford.edu>, Greg Hutchins
12 <gmh@leland.stanford.edu>, Yu Ping Hu <yph@cs.stanford.edu>.
13 Modified by arens and yph. */
15 /* If you've implemented thread_join(), you can uncomment this. */
16 /*#define THREAD_JOIN_IMPLEMENTED*/
18 /* Uncomment to print progress messages. */
19 /*#define SHOW_PROGRESS*/
21 #include "threads/test.h"
23 #include "threads/synch.h"
24 #include "threads/thread.h"
26 static thread_func io_thread;
27 static thread_func cpu_thread;
28 static thread_func io_cpu_thread;
33 static const thread_func *funcs[] = {io_thread, cpu_thread, io_cpu_thread};
34 static const char *names[] = {"IO", "CPU", "IO & CPU"};
35 struct semaphore done[3];
39 "Testing multilevel feedback queue scheduler.\n");
42 for (i = 0; i < 3; i++)
44 sema_init (&done[i], 0);
45 tids[i] = thread_create (names[i], PRI_DEFAULT, funcs[i], &done[i]);
48 /* Wait for threads to finish. */
49 for (i = 0; i < 3; i++)
51 #ifdef THREAD_JOIN_IMPLEMENTED
52 thread_join (tids[i]);
57 printf ("Multilevel feedback queue scheduler test done.\n");
61 cpu_thread (void *sema_)
63 struct semaphore *sema = sema_;
64 int64_t start = timer_ticks ();
68 lock_init (&lock, "cpu");
70 for (i = 0; i < 5000; i++)
74 printf ("CPU intensive: %d\n", thread_get_priority ());
79 printf ("CPU bound thread finished in %"PRI64d" ticks.\n",
80 timer_elapsed (start));
86 io_thread (void *sema_)
88 struct semaphore *sema = sema_;
89 int64_t start = timer_ticks ();
92 for (i = 0; i < 1000; i++)
96 printf ("IO intensive: %d\n", thread_get_priority ());
100 printf ("IO bound thread finished in %"PRI64d" ticks.\n",
101 timer_elapsed (start));
107 io_cpu_thread (void *sema_)
109 struct semaphore *sema = sema_;
111 int64_t start = timer_ticks ();
114 lock_init (&lock, "io & cpu");
116 for (i = 0; i < 800; i++)
122 for (j = 0; j < 15; j++)
124 lock_acquire (&lock);
126 printf ("Alternating IO/CPU: %d\n", thread_get_priority ());
128 lock_release (&lock);
132 printf ("Alternating IO/CPU thread finished in %"PRI64d" ticks.\n",
133 timer_elapsed (start));