/**************************************************************** * sched.c * A simulation of a scheduler via function calls * Written by Robert Hochberg * Last modified: Feb. 27, 2002 * Modified by: * ****************************************************************/ #define MAXNUMSINGERS 50 #include "sched.h" TCB_t *TCB; /* Global Thread Control Block */ int main(int argc, char *argv[]) { /**************************************************************** * A "main" routine for testing your functions ***************************************************************/ TCBinitialize(); /* Initialize this object */ numSingers = atoi(argv[1]); duration = atoi(argv[2]); /**************************************************************** * This is the loop that creates the threads ***************************************************************/ for(i = 0; i < numSingers; i++){ targ[i].duration = duration; targ[i].id = i; targ[i].priority = i % 2; pthread_create(&thrd[i], NULL, sing, (void *) &targ[i]); } /**************************************************************** * Join the threads ***************************************************************/ for(i = 0; i < numSingers; i++) pthread_join(thrd[i], NULL); cout <<"done!" << endl; } /**************************************************************** * The sing function * This is the main routine for each thread * It takes one argument, an integer, to identify the thread * * This is a sample thread that calls the functions. Note how * it uses the time functions. You may want to use such a thing. ****************************************************************/ void *sing(void *arg) { timeval startTime, currentTime; int duration; /* How long thread should run */ int tid; /* id of this thread */ int printflag, printed; /* Used as flags for printing */ int priority; /* Priority of this thread */ duration = ((thread_arg *) arg)->duration; tid = ((thread_arg *) arg)->id; priority = ((thread_arg *) arg)->priority; /* Registers thread with TCB */ thr_register(priority, thr_self()); gettimeofday(&startTime, NULL); gettimeofday(¤tTime, NULL); while(currentTime.tv_sec - startTime.tv_sec < duration){ gettimeofday(¤tTime, NULL); if(currentTime.tv_usec % 200000 < 5000 && !printed) printflag = true; if(currentTime.tv_usec % 200000 >= 5000) printed = false; if(printflag){ cout << "La La La! from thread" << tid << endl; printed = true; printflag = false; } scheduler(); /* Call the scheduler */ } thr_deregister(thr_self()); pthread_exit(NULL); } /**************************************************************** * The register function * Each thread calls this function when it is first created. * The register function is responsible for adding the thread * to the end of the queue to which it belongs. * * Arguments: priority 0 = low, 1 = high * id id of the calling thread ***************************************************************/ void thr_register(int priority, thread_t id){ } /**************************************************************** * The deregister function * Each thread calls this function just before exiting * This function is responsible for deleting the thread from * the queue in which it resides * * Argument: id id of the calling thread ***************************************************************/ void thr_deregister(thread_t id){ } /**************************************************************** * The scheduler function * Responsible for scheduling the threads that main() created. * The number of threads is passed as an argument to scheduler() * Scheduler schedules these threads round-robin fashion, and * joins them all. When the threads have all joined, the * scheduler exits. ***************************************************************/ void scheduler(){ } /**************************************************************** * TCBinitialize * * Creates and initializes the thread control block * ****************************************************************/ void TCBinitialize(){ TCB = new TCB_t; TCB->high = NULL; TCB->low = NULL; TCB->starvationDeadline = 0; TCB->endTime = 0; TCB->currentQueue = -1; }