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. */
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];
39 "Testing multilevel feedback queue scheduler.\n");
42 for (i = 0; i < 3; i++)
44 sema_init (&done[i], 0, names[i]);
45 thread_create (names[i], PRI_DEFAULT, funcs[i], &done[i]);
48 /* Wait for threads to finish. */
49 for (i = 0; i < 3; i++)
51 printf ("Multilevel feedback queue scheduler test done.\n");
55 cpu_thread (void *sema_)
57 struct semaphore *sema = sema_;
58 int64_t start = timer_ticks ();
62 lock_init (&lock, "cpu");
64 for (i = 0; i < 5000; i++)
68 printf ("CPU intensive: %d\n", thread_get_priority ());
73 printf ("CPU bound thread finished in %"PRId64" ticks.\n",
74 timer_elapsed (start));
80 io_thread (void *sema_)
82 struct semaphore *sema = sema_;
83 int64_t start = timer_ticks ();
86 for (i = 0; i < 1000; i++)
90 printf ("IO intensive: %d\n", thread_get_priority ());
94 printf ("IO bound thread finished in %"PRId64" ticks.\n",
95 timer_elapsed (start));
101 io_cpu_thread (void *sema_)
103 struct semaphore *sema = sema_;
105 int64_t start = timer_ticks ();
108 lock_init (&lock, "io & cpu");
110 for (i = 0; i < 800; i++)
116 for (j = 0; j < 15; j++)
118 lock_acquire (&lock);
120 printf ("Alternating IO/CPU: %d\n", thread_get_priority ());
122 lock_release (&lock);
126 printf ("Alternating IO/CPU thread finished in %"PRId64" ticks.\n",
127 timer_elapsed (start));