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 /* Uncomment to print progress messages. */
16 /*#define SHOW_PROGRESS*/
18 #include "threads/test.h"
20 #include "threads/synch.h"
21 #include "threads/thread.h"
23 static thread_func io_thread;
24 static thread_func cpu_thread;
25 static thread_func io_cpu_thread;
30 static const thread_func *funcs[] = {io_thread, cpu_thread, io_cpu_thread};
31 static const char *names[] = {"IO", "CPU", "IO & CPU"};
32 struct semaphore done[3];
36 "Testing multilevel feedback queue scheduler.\n");
39 for (i = 0; i < 3; i++)
41 sema_init (&done[i], 0);
42 tids[i] = thread_create (names[i], PRI_DEFAULT, funcs[i], &done[i]);
45 /* Wait for threads to finish. */
46 for (i = 0; i < 3; i++)
48 #ifdef THREAD_JOIN_IMPLEMENTED
49 thread_join (tids[i]);
54 printf ("Multilevel feedback queue scheduler test done.\n");
58 cpu_thread (void *sema_)
60 struct semaphore *sema = sema_;
61 int64_t start = timer_ticks ();
65 lock_init (&lock, "cpu");
67 for (i = 0; i < 5000; i++)
71 printf ("CPU intensive: %d\n", thread_get_priority ());
76 printf ("CPU bound thread finished in %"PRI64d" ticks.\n",
77 timer_elapsed (start));
83 io_thread (void *sema_)
85 struct semaphore *sema = sema_;
86 int64_t start = timer_ticks ();
89 for (i = 0; i < 1000; i++)
93 printf ("IO intensive: %d\n", thread_get_priority ());
97 printf ("IO bound thread finished in %"PRI64d" ticks.\n",
98 timer_elapsed (start));
104 io_cpu_thread (void *sema_)
106 struct semaphore *sema = sema_;
108 int64_t start = timer_ticks ();
111 lock_init (&lock, "io & cpu");
113 for (i = 0; i < 800; i++)
119 for (j = 0; j < 15; j++)
121 lock_acquire (&lock);
123 printf ("Alternating IO/CPU: %d\n", thread_get_priority ());
125 lock_release (&lock);
129 printf ("Alternating IO/CPU thread finished in %"PRI64d" ticks.\n",
130 timer_elapsed (start));