#include "stdinc.h" #include "uth.h" // // Purpose: Like mileB.c except RR scheduling // 2 threads independently compute fibonacci numbers but // scheduler interrupts periodically to give control to other // thread (i.e., preemption). Also, a high-priority monitor // thread periodically records CPU usage progress // Usage: mileC [-d debug] // Notes: none // Functions: uth_init, uth_create, uth_yield, uth_exit, uth_join, uth_self, // uth_usage // History: // o 10 Apr 2007 kenw: creation // typedef void (func_t)(void); // 0-arg function struct th_arg { int id; int n; }; typedef struct th_arg th_arg_t; // --| constants |-- const int nworkers = 2; // # fibFiber const int TickSz = 10; // tick size (msec); 0 ==> no clock const int Nperiods = 4; // # monitoring periods // --| globals |-- int debug = 0; u_int64_t fib[nworkers]; // result for each thrd uth_tid_t tid[nworkers]; int usage[nworkers][Nperiods]; // --| prototypes |-- void getArgs(int argc, char *argv[]); // --| fib thread |-- // compute fibonacci number // fib(5) = fib(4) + fib(3) // = fib (3) + fib(2) + fib(2) + fib(1) // = fib (2) + fib(1) + fib(2) + fib(2) + fib(1) // = fib (1) + fib(1) + fib(1) + fib(1) + fib(1) + 3 fib(0) // = 5 void fibFiber( void *myarg ) { int whoami = ((th_arg_t *)myarg)->id; int n = ((th_arg_t *)myarg)->n; int ncall = 0; u_int64_t a, b, tmp; printf( "+ BEGIN fibFiber %d (tid %d), n = %d\n", whoami, uth_self(), n ); for (int i=0; i<1000; i++) { // compute 1000 times if ( n == 0) b = 0; else { a = 0; b = 1; for (int i=1; i