Example #1
0
/* Initilize the Monitor
 * we create an index, then the enterSem and urgentSem with
 * values 1, 0 respectivly.  Then the condition variable semaphore
 * and List are created as array sizes K which are givent to the
 * initilizing function.  The semaphore for condition variable
 * is given 0 for every spot in the array.
 * Then the enterQueue and urgentQueue are created */
void MonInit(int k){
  int i;
  enterSem = NewSem(1);
  urgentSem = NewSem(0);
  condSems = (int*)malloc(sizeof(int)*k);
  condLists = (LIST**)malloc(sizeof(LIST *)*k);
  for(i=0; i<k; i++){
    condSems[i] = NewSem(0);
    condLists[i] = ListCreate();
  }
  enterQueue = ListCreate();
  urgentQueue = ListCreate();
}
int main()
{
	printf("Creating global RunQ and Semaphore\n");
	
	//Global RunQ
	TCB_t *RunQ=0;
	semaphore = NewSem();
	
	InitQueue(&RunQ); //Initialize an empty queue
	
	InitQueue(&(semaphore->RunSemQ));	// TODO: Change name?
	InitSem(semaphore, SEMAPHORE_SIZE);
	
	printf("Initializing threads.  This represents building factories...\n");
	start_thread(thread1);//Factory1 
	start_thread(thread2);//Factory2 
	start_thread(thread3);//Factory3 
//	start_thread(thread4);//Factory4 
//	start_thread(thread5);//Factory5
	printf("Threads initialized.  Running now.\n");
	
	run();//Let the magic happen
	return 0;
}